Revision 3146

View differences:

org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2
    
3
    <modelVersion>4.0.0</modelVersion>
4
    <artifactId>org.gvsig.jvmpreferences.native</artifactId>
5
    <packaging>pom</packaging>
6
    <version>1.0.127</version>
7
    <name>${project.artifactId}</name>
8
    <description>Provides preferences for system-dependent issues such as memory management.</description>
9
    
10
    <parent>
11
        <groupId>org.gvsig</groupId>
12
        <artifactId>org.gvsig.desktop</artifactId>
13
        <version>2.0.311</version>
14
    </parent>
15
    
16
    <url>https://devel.gvsig.org/svn/gvsig-projects-pool/org.gvsig.jvmpreferences/trunk/org.gvsig.jvmpreferences.native</url>
17
    <scm>
18
        <connection>scm:svn:https://devel.gvsig.org/svn/gvsig-projects-pool/org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127</connection>
19
        <developerConnection>scm:svn:https://devel.gvsig.org/svn/gvsig-projects-pool/org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127</developerConnection>
20
        <url>https://devel.gvsig.org/svn/gvsig-projects-pool/org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127</url>
21
    </scm>
22
    <repositories>
23
      <repository>
24
        <id>gvsig-public-http-repository</id>
25
        <name>gvSIG maven public HTTP repository</name>
26
        <url>http://devel.gvsig.org/m2repo/j2se</url>
27
        <releases>
28
          <enabled>true</enabled>
29
          <updatePolicy>daily</updatePolicy>
30
          <checksumPolicy>warn</checksumPolicy>
31
        </releases>
32
        <snapshots>
33
          <enabled>true</enabled>
34
          <updatePolicy>daily</updatePolicy>
35
          <checksumPolicy>warn</checksumPolicy>
36
        </snapshots>
37
      </repository>
38
    </repositories>
39

  
40
    <distributionManagement>
41
      <site>
42
        <id>gvsig-repository</id>
43
        <url>dav:https://devel.gvsig.org/sites/${project.artifactId}/${project.version}</url>
44
      </site>
45
    </distributionManagement>
46
 
47

  
48
	<build>
49
		<plugins>
50
			<plugin>
51
				<groupId>org.apache.maven.plugins</groupId>
52
				<artifactId>maven-release-plugin</artifactId>
53
				<configuration>
54
					<tagBase>https://devel.gvsig.org/svn/gvsig-projects-pool/org.gvsig.jvmpreferences/tags</tagBase>
55
				</configuration>
56
			</plugin>
57
		</plugins>
58
	</build>
59
	<modules>
60
		<module>org.gvsig.jvmpreferences.native.mainplugin</module>
61
		<module>org.gvsig.jvmpreferences.native.lib</module>
62
	</modules>
63

  
64
</project>
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.windows/src/test/resources/README.txt
1
Put into this folder the resources needed by your test classes.
2

  
3
This folder is added to the Tests classpath, so you can load any resources 
4
through the ClassLoader.
5

  
6
By default, in this folder you can find an example of log4j configuration,
7
prepared to log messages through the console, so logging works when you
8
run your tests classes.
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.windows/src/test/resources/log4j.xml
1
<?xml version="1.0" encoding="ISO-8859-1" ?>
2
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3

  
4
<!-- 
5
Log4J configuration file for unit tests execution.
6
 -->
7
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
8

  
9
	<!-- Appender configuration to show logging messages through the console -->
10
	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
11
		<layout class="org.apache.log4j.PatternLayout">
12
			<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{2}.%M()]\n  %m%n" />
13
		</layout>
14
	</appender>
15

  
16
	<!-- 
17
	Activate logging messages of DEBUG level of higher only for the
18
	org.gvsig.tools packages.
19
	You can put full classes names or packages instead, to configure
20
	logging for all the classes and subpackages of the package.
21
	-->
22
	<category name="org.gvsig.tools">
23
		<priority value="DEBUG" />
24
	</category>
25
	<category name="org.gvsig.app.document.layout">
26
		<priority value="DEBUG" />
27
	</category>
28

  
29
	<!-- 
30
	By default, show only logging messages of INFO level or higher, 
31
	through the previously configured CONSOLE appender. 
32
	-->
33
	<root>
34
		<priority value="INFO" />
35
		<appender-ref ref="CONSOLE" />
36
	</root>
37
</log4j:configuration>
0 38

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.windows/src/main/java/org/gvsig/jvmpreferences/nativeprefs/NativePrefsManagerWindows.java
1
package org.gvsig.jvmpreferences.nativeprefs;
2

  
3
import org.gvsig.jvmpreferences.nativeprefs.MemoryPreferences;
4
import org.gvsig.jvmpreferences.nativeprefs.NativePrefsManager;
5

  
6
/**
7
 * Windows implementation for NativePrefsManager. 
8
 * 
9
 * @author Cesar Martinez Izquierdo
10
 *
11
 */
12
public class NativePrefsManagerWindows implements NativePrefsManager {
13
	private MemoryPreferences memoryPrefs;
14

  
15
	public synchronized MemoryPreferences getMemoryPreferences() {
16
		if (memoryPrefs==null) {
17
			memoryPrefs = new MemoryPreferencesWindows();
18
		}
19
		return memoryPrefs;
20
	}
21

  
22
}
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.windows/src/main/java/org/gvsig/jvmpreferences/nativeprefs/MemoryPreferencesWindows.java
1
package org.gvsig.jvmpreferences.nativeprefs;
2

  
3

  
4
import java.io.BufferedReader;
5
import java.io.BufferedWriter;
6
import java.io.File;
7
import java.io.FileNotFoundException;
8
import java.io.FileReader;
9
import java.io.FileWriter;
10
import java.io.IOException;
11
import java.lang.management.ManagementFactory;
12
import java.nio.file.Files;
13
import java.text.SimpleDateFormat;
14
import java.util.Date;
15

  
16
import javax.management.AttributeNotFoundException;
17
import javax.management.InstanceNotFoundException;
18
import javax.management.MBeanException;
19
import javax.management.MBeanServer;
20
import javax.management.MalformedObjectNameException;
21
import javax.management.ObjectName;
22
import javax.management.ReflectionException;
23

  
24
import org.gvsig.andami.Launcher;
25
import org.gvsig.i18n.Messages;
26
import org.gvsig.jvmpreferences.nativeprefs.BaseMemoryPreferences;
27
import org.gvsig.utils.DateTime;
28
import org.slf4j.LoggerFactory;
29

  
30
/**
31
 * This class abstracts the complexity of reading/storing
32
 * memory preferences for different operating systems.
33
 * Windows implementation
34
 *
35
 * @author Cesar Martinez Izquierdo
36
 *
37
 */
38
public class MemoryPreferencesWindows extends BaseMemoryPreferences {
39
//	public static final String CONFIG_FILE_WINDOWS="gvSIG.sh";
40
//	public static final String CONFIG_FILE_WINDOWS="gvsig-desktop.l4j.ini";
41
    public static final String CONFIG_FILE_WINDOWS="gvSIG.config";
42
	private File configFile;
43

  
44
	public MemoryPreferencesWindows() {
45
		super();
46
		configFile = new File(Launcher.getApplicationDirectory() + File.separator + CONFIG_FILE_WINDOWS);
47
	}
48

  
49
	public File getConfigFile() {
50
		return configFile;
51
	}
52

  
53
	/* (non-Javadoc)
54
	 * @see org.gvsig.preferences.nativeprefs.general.memory.MemoryPreferences#isConfigWritable()
55
	 */
56
	public boolean isConfigWritable(){
57
		// We can't use getConfigFile().canWrite() because the file
58
		// is writable *at file system level* (so this method returns true),
59
		// but writing fails as the user has no effective permissions (on Win7)
60
		try {
61
			File f = getConfigFile();
62
			File test = new File(f.getAbsolutePath()+".test");
63
			if (f.renameTo(test) && test.renameTo(f)) {
64
				return true;
65
			}
66
		}
67
		catch (Exception ex) {
68
		}
69
		return false;
70
	}
71

  
72
//	@Override
73
//	protected String getMaxMemoryRegexp() {
74
//		return "\\s*#?\\s*-Xmx(\\d+)([mMkKgG]?)\\s*";
75
//	}
76
//
77
//	@Override
78
//	protected String getEncodedMaxMemoryLine(int maximumMemory) {
79
//		return " -Xmx"+maximumMemory+"m";
80
//	}
81
//
82
	@Override
83
	protected String getMaxMemoryRegexp() {
84
		//return "\\s*export\\s+GVSIG\\_MAX\\_MEM\\s*\\=\\s*(\\d+)([mMkKgG]?).*";
85
	    return "\\s*GVSIG\\_MAX\\_MEM\\s*\\=\\s*(\\d+)([mMkKgG]?).*";
86
	}
87

  
88
	@Override
89
	protected String getEncodedMaxMemoryLine(int maximumMemory) {
90
		//return "export GVSIG_MAX_MEM="+maximumMemory+"M";
91
	    return "GVSIG_MAX_MEM="+maximumMemory+"M";
92
	}
93
	
94
	@Override
95
	protected String getNewLineChars() {
96
		return "\n";
97
	}
98
}
0 99

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.windows/pom.xml
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<artifactId>org.gvsig.jvmpreferences.native.lib.impl.windows</artifactId>
5
        <packaging>jar</packaging>
6
        <name>${project.artifactId}</name>
7
	<parent>
8
		<groupId>org.gvsig</groupId>
9
		<artifactId>org.gvsig.jvmpreferences.native.lib</artifactId>
10
		<version>1.0.127</version>
11
	</parent>
12
	<dependencies>
13
		<dependency>
14
			<groupId>org.gvsig</groupId>
15
			<artifactId>org.gvsig.jvmpreferences.native.lib.api</artifactId>
16
			<scope>compile</scope>
17
			<version>1.0.127</version>
18
		</dependency>
19
		<dependency>
20
			<groupId>org.gvsig</groupId>
21
			<artifactId>org.gvsig.jvmpreferences.native.lib.impl.common</artifactId>
22
			<scope>compile</scope>
23
			<version>1.0.127</version>
24
		</dependency>
25
		<dependency>
26
			<groupId>org.gvsig</groupId>
27
			<artifactId>org.gvsig.i18n</artifactId>
28
			<scope>compile</scope>
29
		</dependency>
30
		<dependency>
31
			<groupId>org.gvsig</groupId>
32
			<artifactId>org.gvsig.andami</artifactId>
33
			<scope>compile</scope>
34
		</dependency>
35
		<dependency>
36
			<groupId>org.gvsig</groupId>
37
			<artifactId>org.gvsig.app.mainplugin</artifactId>
38
			<scope>compile</scope>
39
		</dependency>
40
	</dependencies>
41
</project>
0 42

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.linux/src/test/resources/README.txt
1
Put into this folder the resources needed by your test classes.
2

  
3
This folder is added to the Tests classpath, so you can load any resources 
4
through the ClassLoader.
5

  
6
By default, in this folder you can find an example of log4j configuration,
7
prepared to log messages through the console, so logging works when you
8
run your tests classes.
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.linux/src/test/resources/log4j.xml
1
<?xml version="1.0" encoding="ISO-8859-1" ?>
2
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3

  
4
<!-- 
5
Log4J configuration file for unit tests execution.
6
 -->
7
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
8

  
9
	<!-- Appender configuration to show logging messages through the console -->
10
	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
11
		<layout class="org.apache.log4j.PatternLayout">
12
			<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{2}.%M()]\n  %m%n" />
13
		</layout>
14
	</appender>
15

  
16
	<!-- 
17
	Activate logging messages of DEBUG level of higher only for the
18
	org.gvsig.tools packages.
19
	You can put full classes names or packages instead, to configure
20
	logging for all the classes and subpackages of the package.
21
	-->
22
	<category name="org.gvsig.tools">
23
		<priority value="DEBUG" />
24
	</category>
25
	<category name="org.gvsig.app.document.layout">
26
		<priority value="DEBUG" />
27
	</category>
28

  
29
	<!-- 
30
	By default, show only logging messages of INFO level or higher, 
31
	through the previously configured CONSOLE appender. 
32
	-->
33
	<root>
34
		<priority value="INFO" />
35
		<appender-ref ref="CONSOLE" />
36
	</root>
37
</log4j:configuration>
0 38

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.linux/src/main/java/org/gvsig/jvmpreferences/nativeprefs/NativePrefsManagerLinux.java
1
package org.gvsig.jvmpreferences.nativeprefs;
2

  
3
import org.gvsig.jvmpreferences.nativeprefs.MemoryPreferences;
4
import org.gvsig.jvmpreferences.nativeprefs.NativePrefsManager;
5

  
6
/**
7
 * Linux implementation for NativePrefsManager
8
 * 
9
 * @author Cesar Martinez Izquierdo
10
 *
11
 */
12
public class NativePrefsManagerLinux implements NativePrefsManager {
13
	private MemoryPreferences memoryPrefs;
14

  
15
	public synchronized MemoryPreferences getMemoryPreferences() {
16
		if (memoryPrefs==null) {
17
			memoryPrefs = new MemoryPreferencesLinux();
18
		}
19
		return memoryPrefs;
20
	}
21

  
22
}
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.linux/src/main/java/org/gvsig/jvmpreferences/nativeprefs/MemoryPreferencesLinux.java
1
package org.gvsig.jvmpreferences.nativeprefs;
2

  
3

  
4
import java.io.BufferedReader;
5
import java.io.BufferedWriter;
6
import java.io.File;
7
import java.io.FileNotFoundException;
8
import java.io.FileReader;
9
import java.io.FileWriter;
10
import java.io.IOException;
11
import java.lang.management.ManagementFactory;
12
import java.text.SimpleDateFormat;
13
import java.util.Date;
14

  
15
import javax.management.AttributeNotFoundException;
16
import javax.management.InstanceNotFoundException;
17
import javax.management.MBeanException;
18
import javax.management.MBeanServer;
19
import javax.management.MalformedObjectNameException;
20
import javax.management.ObjectName;
21
import javax.management.ReflectionException;
22

  
23
import org.gvsig.andami.Launcher;
24
import org.gvsig.app.ApplicationLocator;
25
import org.gvsig.i18n.Messages;
26
import org.gvsig.jvmpreferences.nativeprefs.BaseMemoryPreferences;
27
import org.gvsig.tools.swing.api.ToolsSwingLocator;
28
import org.slf4j.LoggerFactory;
29

  
30
/**
31
 * This class abstracts the complexity of reading/storing
32
 * memory preferences for different operating systems.
33
 * Linux implementation
34
 *
35
 * @author Cesar Martinez Izquierdo
36
 *
37
 */
38
public class MemoryPreferencesLinux extends BaseMemoryPreferences {
39
	//public static final String CONFIG_FILE_LINUX="gvSIG.sh";
40
    public static final String CONFIG_FILE_LINUX="gvSIG.config";
41
	private File configFile;
42

  
43
	public MemoryPreferencesLinux() {
44
		super();
45
		configFile = new File(Launcher.getApplicationDirectory() + File.separator + CONFIG_FILE_LINUX);
46
	}
47

  
48
	public File getConfigFile() {
49
		return configFile;
50
	}
51

  
52
	@Override
53
	protected String getMaxMemoryRegexp() {
54
		//return "\\s*export\\s+GVSIG\\_MAX\\_MEM\\s*\\=\\s*(\\d+)([mMkKgG]?).*";
55
	    return "\\s*GVSIG\\_MAX\\_MEM\\s*\\=\\s*(\\d+)([mMkKgG]?).*";
56
	}
57

  
58
	@Override
59
	protected String getEncodedMaxMemoryLine(int maximumMemory) {
60
		//return "export GVSIG_MAX_MEM="+maximumMemory+"M";
61
	    return "GVSIG_MAX_MEM="+maximumMemory+"M";
62
	}
63
	
64
	@Override
65
	protected String getNewLineChars() {
66
		return "\n";
67
	}
68

  
69
}
0 70

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.linux/pom.xml
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<artifactId>org.gvsig.jvmpreferences.native.lib.impl.linux</artifactId>
5
        <packaging>jar</packaging>
6
	<name>${project.artifactId}</name>
7
	<parent>
8
		<groupId>org.gvsig</groupId>
9
		<artifactId>org.gvsig.jvmpreferences.native.lib</artifactId>
10
		<version>1.0.127</version>
11
	</parent>
12
	<dependencies>
13
		<dependency>
14
			<groupId>org.gvsig</groupId>
15
			<artifactId>org.gvsig.jvmpreferences.native.lib.api</artifactId>
16
			<scope>compile</scope>
17
			<version>1.0.127</version>
18
		</dependency>
19
		<dependency>
20
			<groupId>org.gvsig</groupId>
21
			<artifactId>org.gvsig.jvmpreferences.native.lib.impl.common</artifactId>
22
			<scope>compile</scope>
23
			<version>1.0.127</version>
24
		</dependency>
25
		<dependency>
26
			<groupId>org.gvsig</groupId>
27
			<artifactId>org.gvsig.i18n</artifactId>
28
			<scope>compile</scope>
29
		</dependency>
30
		<dependency>
31
			<groupId>org.gvsig</groupId>
32
			<artifactId>org.gvsig.andami</artifactId>
33
			<scope>compile</scope>
34
		</dependency>
35
		<dependency>
36
			<groupId>org.gvsig</groupId>
37
			<artifactId>org.gvsig.app.mainplugin</artifactId>
38
			<scope>compile</scope>
39
		</dependency>
40
	</dependencies>
41
</project>
0 42

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2

  
3
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
    
5
    <modelVersion>4.0.0</modelVersion>
6
    <packaging>pom</packaging>
7
    <artifactId>org.gvsig.jvmpreferences.native.lib</artifactId>
8
    <name>org.gvsig.jvmpreferences.native.lib</name>
9
    
10
    <parent>
11
        <groupId>org.gvsig</groupId>
12
        <artifactId>org.gvsig.jvmpreferences.native</artifactId>
13
        <version>1.0.127</version>
14
    </parent>
15
    <modules>
16
        <module>org.gvsig.jvmpreferences.native.lib.api</module>
17
        <module>org.gvsig.jvmpreferences.native.lib.impl.common</module>
18
        <module>org.gvsig.jvmpreferences.native.lib.impl.windows</module>
19
        <module>org.gvsig.jvmpreferences.native.lib.impl.linux</module>
20
     </modules>
21

  
22
</project>
23

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.common/src/test/resources/README.txt
1
Put into this folder the resources needed by your test classes.
2

  
3
This folder is added to the Tests classpath, so you can load any resources 
4
through the ClassLoader.
5

  
6
By default, in this folder you can find an example of log4j configuration,
7
prepared to log messages through the console, so logging works when you
8
run your tests classes.
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.common/src/test/resources/log4j.xml
1
<?xml version="1.0" encoding="ISO-8859-1" ?>
2
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3

  
4
<!-- 
5
Log4J configuration file for unit tests execution.
6
 -->
7
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
8

  
9
	<!-- Appender configuration to show logging messages through the console -->
10
	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
11
		<layout class="org.apache.log4j.PatternLayout">
12
			<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{2}.%M()]\n  %m%n" />
13
		</layout>
14
	</appender>
15

  
16
	<!-- 
17
	Activate logging messages of DEBUG level of higher only for the
18
	org.gvsig.tools packages.
19
	You can put full classes names or packages instead, to configure
20
	logging for all the classes and subpackages of the package.
21
	-->
22
	<category name="org.gvsig.tools">
23
		<priority value="DEBUG" />
24
	</category>
25
	<category name="org.gvsig.app.document.layout">
26
		<priority value="DEBUG" />
27
	</category>
28

  
29
	<!-- 
30
	By default, show only logging messages of INFO level or higher, 
31
	through the previously configured CONSOLE appender. 
32
	-->
33
	<root>
34
		<priority value="INFO" />
35
		<appender-ref ref="CONSOLE" />
36
	</root>
37
</log4j:configuration>
0 38

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.common/src/main/java/org/gvsig/jvmpreferences/nativeprefs/BaseMemoryPreferences.java
1
package org.gvsig.jvmpreferences.nativeprefs;
2

  
3

  
4
import java.io.BufferedInputStream;
5
import java.io.BufferedOutputStream;
6
import java.io.BufferedReader;
7
import java.io.BufferedWriter;
8
import java.io.File;
9
import java.io.FileInputStream;
10
import java.io.FileNotFoundException;
11
import java.io.FileOutputStream;
12
import java.io.FileReader;
13
import java.io.FileWriter;
14
import java.io.IOException;
15
import java.io.InputStream;
16
import java.io.OutputStream;
17
import java.lang.management.ManagementFactory;
18
import java.text.SimpleDateFormat;
19
import java.util.Date;
20
import java.util.regex.Matcher;
21
import java.util.regex.Pattern;
22

  
23
import javax.management.AttributeNotFoundException;
24
import javax.management.InstanceNotFoundException;
25
import javax.management.MBeanException;
26
import javax.management.MBeanServer;
27
import javax.management.MalformedObjectNameException;
28
import javax.management.ObjectName;
29
import javax.management.ReflectionException;
30

  
31
import org.gvsig.i18n.Messages;
32
import org.gvsig.jvmpreferences.nativeprefs.MemoryPreferences;
33
import org.slf4j.LoggerFactory;
34

  
35
/**
36
 * This class abstracts the complexity of reading/storing
37
 * memory preferences for different operating systems
38
 * 
39
 * @author Cesar Martinez Izquierdo
40
 *
41
 */
42
public abstract class BaseMemoryPreferences implements MemoryPreferences {
43
	public static final int FACTORY_DEFAULT_MAXIMUM = 512;
44
	
45
	/* (non-Javadoc)
46
	 * @see org.gvsig.preferences.nativeprefs.general.memory.MemoryPreferences#getConfigFile()
47
	 */
48
	public abstract File getConfigFile();
49
	
50
	/* (non-Javadoc)
51
	 * @see org.gvsig.preferences.nativeprefs.general.memory.MemoryPreferences#isConfigWritable()
52
	 */
53
	public boolean isConfigWritable(){
54
		return getConfigFile().canWrite();
55
	}
56
	
57
	/* (non-Javadoc)
58
	 * @see org.gvsig.preferences.nativeprefs.general.memory.MemoryPreferences#getTotalSystemMemory()
59
	 */
60
	public int getTotalSystemMemory() {
61
		MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
62
		Object attribute;
63
		try {
64
			attribute = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "TotalPhysicalMemorySize");
65
			System.out.println("Total memory: "+ attribute.toString() +" B");
66
			System.out.println("Total memory: "+ Long.parseLong(attribute.toString())/(1024*1024) +" MB");
67
			return (int) (Long.parseLong(attribute.toString())/(1024*1024));
68
		} catch (AttributeNotFoundException e) {
69
			LoggerFactory.getLogger(BaseMemoryPreferences.class).error("Total_system_memory_cant_be_retrived", e);
70
		} catch (InstanceNotFoundException e) {
71
			LoggerFactory.getLogger(BaseMemoryPreferences.class).error("Total_system_memory_cant_be_retrived", e);
72
		} catch (MalformedObjectNameException e) {
73
			LoggerFactory.getLogger(BaseMemoryPreferences.class).error("Total_system_memory_cant_be_retrived", e);
74
		} catch (MBeanException e) {
75
			LoggerFactory.getLogger(BaseMemoryPreferences.class).error("Total_system_memory_cant_be_retrived", e);
76
		} catch (ReflectionException e) {
77
			LoggerFactory.getLogger(BaseMemoryPreferences.class).error("Total_system_memory_cant_be_retrived", e);
78
		} catch (NullPointerException e) {
79
			LoggerFactory.getLogger(BaseMemoryPreferences.class).error("Total_system_memory_cant_be_retrived", e);
80
		}
81
		return 512;
82
	}
83
	
84
	/* (non-Javadoc)
85
	 * @see org.gvsig.preferences.nativeprefs.general.memory.MemoryPreferences#getSafeMemoryMaximum()
86
	 */
87
	public int getSafeMemoryMaximum() {
88
		int totalMemory = getTotalSystemMemory();
89
		if (totalMemory<=1024) {
90
			return (int)(0.6*(float)totalMemory);
91
		}
92
		String arch = System.getProperty("os.arch");
93
		if ("x86".equals(arch) || "i386".equals(arch)) {
94
			return 1200;
95
		}
96
		return (totalMemory-768);
97
	}
98
	
99
	/* (non-Javadoc)
100
	 * @see org.gvsig.preferences.nativeprefs.general.memory.MemoryPreferences#readConfiguredMaximum()
101
	 */
102
	public int readConfiguredMaximum() {
103
		File launcherFile = getConfigFile();
104
		if (launcherFile.exists()) {
105
			BufferedReader reader = null;
106
			try {
107
				reader = new BufferedReader(new FileReader(launcherFile));
108
				String line;
109
				Pattern pattern = Pattern.compile(getMaxMemoryRegexp());
110
				while ((line = reader.readLine())!=null) {
111
					Matcher m = pattern.matcher(line);
112
					if (m.matches()) {
113
						String memoryStr=m.group(1);
114
						String units = m.group(2);
115
						int configMemory;
116
						try {
117
							// first of all assume it does not have units
118
							configMemory = Integer.parseInt(memoryStr);
119
							System.out.println("Configured memory: " + configMemory);
120
							if (units!=null) {
121
								units = units.toLowerCase();
122
								if ("g".equals(units)) {
123
									configMemory = configMemory * 1024;
124
								}
125
								else if ("k".equals(units)) {
126
									configMemory = (int)((float)configMemory/1024.0);
127
								}
128
							}
129
							return configMemory;
130
						}
131
						catch (NumberFormatException ex) {
132
						}
133
					}
134
				}
135
			} catch (FileNotFoundException e) {
136
				LoggerFactory.getLogger(this.getClass()).error(Messages.getText("Memory_config_could_not_be_retrived"), e);
137
			} catch (NumberFormatException e) {
138
				LoggerFactory.getLogger(this.getClass()).error(Messages.getText("Memory_config_could_not_be_retrived"), e);
139
			} catch (IOException e) {
140
				LoggerFactory.getLogger(this.getClass()).error(Messages.getText("Memory_config_could_not_be_retrived"), e);
141
			}
142
			finally {
143
				try {
144
					if (reader!=null) {
145
						reader.close();
146
					}
147
				} catch (IOException e) {
148
				}
149
			}
150
		}
151
		LoggerFactory.getLogger(this.getClass()).error(Messages.getText("Memory_config_could_not_be_retrived"));
152
		return getSafeMemoryMaximum();
153
	}
154
	
155
	/**
156
	 * Copies one file. We are not using
157
	 * {@link org.gvsig.utils.FileUtils#copy(File, File)} method
158
	 * because it does not use buffered reader. Probably it should be corrected
159
	 * there and removed from here
160
	 * 
161
	 * @param src The file to copy
162
	 * @param dst The destination file
163
	 * @throws IOException
164
	 */
165
    public static void copy(File src, File dst) throws IOException {
166
        InputStream in = new BufferedInputStream(new FileInputStream(src));
167
        OutputStream out = new BufferedOutputStream(new FileOutputStream(dst));
168
    
169
        // Transfer bytes from in to out
170
        byte[] buf = new byte[10240];
171
        int len;
172
        while ((len = in.read(buf)) > 0) {
173
            out.write(buf, 0, len);
174
        }
175
        in.close();
176
        out.close();
177
    }
178

  
179
	
180
	/* (non-Javadoc)
181
	 * @see org.gvsig.preferences.nativeprefs.general.memory.MemoryPreferences#saveConguredMaximum()
182
	 */
183
	public void saveConguredMaximum(int maximumMemory) {
184
		File launcherFile = getConfigFile();
185
		if (launcherFile.exists()) {
186
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
187
			String date = format.format(new Date());
188
			File backupFile = new File(launcherFile.getAbsolutePath()+"-"+date+".bak");
189
			
190
			BufferedWriter writer = null;
191
			BufferedReader reader = null;
192
			
193
			try {
194
				copy(launcherFile, backupFile);
195
				writer = new BufferedWriter(new FileWriter(launcherFile));
196
				reader = new BufferedReader(new FileReader(backupFile));
197
				String line;
198
				Pattern pattern = Pattern.compile(getMaxMemoryRegexp());
199
				boolean written = false;
200
				while ((line = reader.readLine())!=null) {
201
					if (!pattern.matcher(line).matches() || written) {
202
						writer.write(line);
203
						writer.write(getNewLineChars());
204
					}
205
					else {
206
						written = true; // avoid writing the mem line twice
207
						writer.write(getEncodedMaxMemoryLine(maximumMemory));
208
						writer.write(getNewLineChars());
209
					}
210
				}
211
			} catch (FileNotFoundException e) {
212
				LoggerFactory.getLogger(this.getClass()).error(Messages.getText("Memory_config_could_not_be_written"), e);
213
			} catch (IOException e) {
214
				LoggerFactory.getLogger(this.getClass()).error(Messages.getText("Memory_config_could_not_be_written"), e);
215
			}
216
			finally {
217
				if (reader!=null) {
218
					try {
219
						reader.close();
220
					} catch (IOException e) {
221
						LoggerFactory.getLogger(this.getClass()).debug(Messages.getText("Error_closing_reader"), e);
222
					}
223
				}
224
				if (writer!=null) {
225
					try {
226
						writer.close();
227
					} catch (IOException e) {
228
						LoggerFactory.getLogger(this.getClass()).debug(Messages.getText("Error_closing_writer"), e);
229
					}
230
				}
231
			}
232
		}
233
	}
234
	
235
	/**
236
	 * A valid Java regular expression that matches the line
237
	 * containing the maximum memory parameter (-Xmx) parameter
238
	 * on the launcher file for this platform.
239
	 * 
240
	 * The regular expression must contain 2 capturing groups,
241
	 * the first one capturing the numeric part and the second
242
	 * one capturing the units.
243
	 * 
244
	 * E.g. for the definition -Xmx1024M,
245
	 * the first group must capture '1024', while the second
246
	 * group must capture the 'M'.
247
	 * 
248
	 * Note that the units may be absent in the definition string.
249
	 * 
250
	 * @return A valid Java regular expression
251
	 */
252
	protected abstract String getMaxMemoryRegexp();
253
	
254
	/**
255
	 * Encodes the maximum memory parameter (-Xmx) in a suitable format
256
	 * for being directly written on the launcher configuration files
257
	 * for this platform.
258
	 *  
259
	 * @param maximumMemory
260
	 * @return
261
	 */
262
	protected abstract String getEncodedMaxMemoryLine(int maximumMemory);
263
	
264
	/**
265
	 * Gets the character(s) used as line separators. We don't use the "line.separator"
266
	 * Java property because we currently must use the Unix-style separator even in Windows,
267
	 * and this might change in the future.
268
	 * @return
269
	 */
270
	protected abstract String getNewLineChars();
271
	
272
	public int getFactoryDefaultMaximum() {
273
		return FACTORY_DEFAULT_MAXIMUM;
274
	}
275
}
0 276

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.impl.common/pom.xml
1
<?xml version="1.0" encoding="ISO-8859-1"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
	<modelVersion>4.0.0</modelVersion>
4
	<artifactId>org.gvsig.jvmpreferences.native.lib.impl.common</artifactId>
5
        <packaging>jar</packaging>
6
	<name>${project.artifactId}</name>
7
	<parent>
8
		<groupId>org.gvsig</groupId>
9
		<artifactId>org.gvsig.jvmpreferences.native.lib</artifactId>
10
		<version>1.0.127</version>
11
	</parent>
12
	<dependencies>
13
		<dependency>
14
			<groupId>org.gvsig</groupId>
15
			<artifactId>org.gvsig.jvmpreferences.native.lib.api</artifactId>
16
			<scope>compile</scope>
17
			<version>1.0.127</version>
18
		</dependency>
19
		<dependency>
20
			<groupId>org.gvsig</groupId>
21
			<artifactId>org.gvsig.i18n</artifactId>
22
			<scope>compile</scope>
23
		</dependency>
24
		<dependency>
25
			<groupId>org.gvsig</groupId>
26
			<artifactId>org.gvsig.andami</artifactId>
27
			<scope>compile</scope>
28
		</dependency>
29
		<dependency>
30
			<groupId>org.gvsig</groupId>
31
			<artifactId>org.gvsig.app.mainplugin</artifactId>
32
			<scope>compile</scope>
33
		</dependency>
34
	</dependencies>
35
</project>
0 36

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.api/src/test/resources/README.txt
1
Put into this folder the resources needed by your test classes.
2

  
3
This folder is added to the Tests classpath, so you can load any resources 
4
through the ClassLoader.
5

  
6
By default, in this folder you can find an example of log4j configuration,
7
prepared to log messages through the console, so logging works when you
8
run your tests classes.
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.api/src/test/resources/log4j.xml
1
<?xml version="1.0" encoding="ISO-8859-1" ?>
2
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3

  
4
<!-- 
5
Log4J configuration file for unit tests execution.
6
 -->
7
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
8

  
9
	<!-- Appender configuration to show logging messages through the console -->
10
	<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
11
		<layout class="org.apache.log4j.PatternLayout">
12
			<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-5p [%c{2}.%M()]\n  %m%n" />
13
		</layout>
14
	</appender>
15

  
16
	<!-- 
17
	Activate logging messages of DEBUG level of higher only for the
18
	org.gvsig.tools packages.
19
	You can put full classes names or packages instead, to configure
20
	logging for all the classes and subpackages of the package.
21
	-->
22
	<category name="org.gvsig.tools">
23
		<priority value="DEBUG" />
24
	</category>
25
	<category name="org.gvsig.app.document.layout">
26
		<priority value="DEBUG" />
27
	</category>
28

  
29
	<!-- 
30
	By default, show only logging messages of INFO level or higher, 
31
	through the previously configured CONSOLE appender. 
32
	-->
33
	<root>
34
		<priority value="INFO" />
35
		<appender-ref ref="CONSOLE" />
36
	</root>
37
</log4j:configuration>
0 38

  
org.gvsig.jvmpreferences/tags/org.gvsig.jvmpreferences.native-1.0.127/org.gvsig.jvmpreferences.native.lib/org.gvsig.jvmpreferences.native.lib.api/src/main/java/org/gvsig/jvmpreferences/nativeprefs/MemoryPreferences.java
1
package org.gvsig.jvmpreferences.nativeprefs;
2

  
3
import java.io.File;
4

  
5
/**
6
 * This class abstracts the complexity of reading/storing
7
 * memory preferences for different operating systems.
8
 * It has methods to
9
 * read and write the configured maximum memory for gvSIG, and it provides
10
 * other convenience methods related with memory management.
11
 * 
12
 * Note that the configuration managed by this interface is system-dependent
13
 * (different configuration files are used on each operating system), so
14
 * different implementations are provided for each system.
15
 * 
16
 * @author Cesar Martinez Izquierdo
17
 *
18
 */
19
public interface MemoryPreferences {
20

  
21
	/**
22
	 * Gets the absolute path to the memory configuration file
23
	 * 
24
	 * @return
25
	 */
26
	public File getConfigFile();
27

  
28
	/**
29
	 * Checks whether the current Java process has write permission on the
30
	 * memory configuration file. As, gvSIG can be installed on a folder
31
	 * having restricted permissions, the file may not be writable unless
32
	 * gvSIG is executed as administrator. This is usually the case in
33
	 * Windows Vista, 7 and 8.
34
	 * 
35
	 * @return Returns <code>true</code> if the configuration file is writable by this process,
36
	 * <code>false</code> otherwise.
37
	 */
38
	public boolean isConfigWritable();
39

  
40
	/**
41
	 * Returns the total amount of RAM memory installed on this computer.
42
	 * 
43
	 * @return The total amount of memory, measured in megabytes
44
	 */
45
	public int getTotalSystemMemory();
46

  
47
	/**
48
	 * Gets the maximum amount of memory (in megabytes) that
49
	 * it is considered to be safe for this computer.
50
	 * 
51
	 * This amount is calculated based on
52
	 * different parameters such as the total amount of available
53
	 * RAM memory on the computer and the architecture of the
54
	 * JVM (32 bits or 64 bits).
55
	 * 
56
	 * Due to the way in which the Java Virtual Machine, it is
57
	 * unsafe to use more than ~1200 MB for 32 bit JVMs, as the
58
	 * JVM needs a contiguous memory space and it will not start
59
	 * at all if it is not available. Note that the availability
60
	 * of a free, contiguous memory space also depends on the
61
	 * previously loaded programs, so an unsafe value may work
62
	 * on one JVM execution and fail on a different one.
63
	 *
64
	 * @return
65
	 */
66
	public int getSafeMemoryMaximum();
67

  
68
	/**
69
	 * Reads the maximum configured memory (-Xmx) from gvSIG
70
	 * launch configuration file
71
	 * 
72
	 * @return The maximum configured memory, measured in megabytes
73
	 */
74
	public int readConfiguredMaximum();
75

  
76
	/**
77
	 * Sets the maximum memory parameter (-Xmx) on the gvSIG
78
	 * launch configuration file. Caution: This method directly writes
79
	 * the parameter to the configuration file.
80
	 * 
81
	 * @param maximumMemory The maximum memory, measured in megabytes
82
	 */
83
	public void saveConguredMaximum(int maximumMemory);
84
	
85
	/**
86
	 * Gets the factory default for maximum memory for gvSIG.
87
	 * 
88
	 * @return
89
	 */
90
	public int getFactoryDefaultMaximum();
91

  
92
}
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff