KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jahia > opentools > advancedapprunner > AdvancedAppRunner


1 package com.jahia.opentools.advancedapprunner;
2
3 import java.io.File JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import com.borland.jbuilder.node.JBProject;
7 import com.borland.jbuilder.runtime.JavaRunner;
8 import com.borland.primetime.PrimeTime;
9 import com.borland.primetime.ide.Browser;
10 import com.borland.primetime.node.Project;
11 import com.borland.primetime.properties.MapArrayProperty;
12 import com.borland.primetime.properties.MapProperty;
13 import com.borland.primetime.properties.PropertyPage;
14 import com.borland.primetime.properties.PropertyPageFactory;
15 import com.borland.primetime.runtime.RuntimeManager;
16 import com.borland.primetime.properties.PropertyManager;
17 import com.borland.jbuilder.paths.ProjectPathSet;
18 import com.borland.jbuilder.paths.JDKPathSet;
19 import com.borland.primetime.properties.MapBooleanProperty;
20
21 /**
22  * A runtime environment for Advanced Application
23  *
24  * @author Keith Wood (kbwood@compuserve.com)
25  * @author Serge Huber (shuber@jahia.com)
26  * @version 1.0 18 May 2002
27  * @version 2.0 22 August 2004
28  */

29 public class AdvancedAppRunner extends JavaRunner {
30
31     public static final String JavaDoc CATEGORY = "runtime";
32
33     private static final MapProperty CUSTOM_JDK_PROP = new MapProperty(CATEGORY, "custom.jdk");
34     private static final MapProperty CUSTOM_OVERRIDE_JDK_PROP = new MapBooleanProperty(CATEGORY, "custom.override_jdk");
35
36     public static final MapProperty MAIN_CLASS = new MapProperty(
37         CATEGORY, "opentools.mainclass",
38         "org.apache.catalina.startup.Bootstrap");
39     public static final MapProperty VM_PARAMS = new MapProperty(
40         CATEGORY, "opentools.vmparameters", "");
41     public static final MapProperty JB_PARAMS = new MapProperty(
42         CATEGORY, "opentools.parameters", "");
43     public static final MapArrayProperty OTHER_DIRS = new MapArrayProperty(
44         CATEGORY, "opentools.otherDirs");
45
46     private static final String JavaDoc VERSION = "2.0";
47
48     /**
49      * Register the OpenTools runtime.
50      *
51      * @param majorVersion the major version of the current OpenTools API
52      * @param minorVersion the minor version of the current OpenTools API
53      */

54     public static void initOpenTool(byte majorVersion, byte minorVersion) {
55         if (majorVersion != PrimeTime.CURRENT_MAJOR_VERSION) {
56             return;
57         }
58         RuntimeManager.registerRunner(new AdvancedAppRunner());
59         if (PrimeTime.isVerbose()) {
60             System.out.println("Loaded Advanced Application runtime v" + VERSION);
61             System.out.println("Written by Serge Huber (shuber@jahia.com)");
62         }
63     }
64
65     /**
66      * Validate the runtime environment.
67      */

68     public boolean isValid(Browser browser, Project project, Map JavaDoc propertyMap,
69                            boolean debug) {
70         return true;
71     }
72
73     /**
74      * Return a factory for an appropriate property page.
75      */

76     public PropertyPageFactory getPageFactory(Project project, Map JavaDoc propertyMap) {
77         if (project instanceof JBProject) {
78             final Project aProject = project;
79             final Map JavaDoc aPropertyMap = propertyMap;
80             return new PropertyPageFactory("Advanced Application",
81                                            "Run an advanced application") {
82                 public PropertyPage createPropertyPage() {
83                     return new AdvancedAppRunnerPropertyPage(aProject,
84                         aPropertyMap);
85                 }
86             };
87         } else {
88             return null;
89         }
90     }
91
92     /**
93      * Update the classpath for this runtime - add all JARs and Zips
94      * from JBuilder/lib, and any other specified directories.
95      */

96     public String JavaDoc getClassPath(JBProject project, Map JavaDoc propertyMap) {
97         String JavaDoc classPath = super.getClassPath(project, propertyMap);
98         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
99         // StringBuffer buffer = new StringBuffer(classPath);
100
//addToClassPath(buffer,
101
// new File(PropertyManager.getInstallRootUrl().getFileObject(), "lib"));
102
SysEnvPropertiesProvider envProvider = SysEnvPropertiesProvider.getInstance();
103         envProvider.setProperty("JBUILDER_HOME", PropertyManager.getInstallRootUrl().getFileObject().toString());
104
105         ProjectPathSet projectPathSet = project.getPaths();
106         JDKPathSet jdkPathSet = projectPathSet.getJDKPathSet();
107         // now let's look at whether the runtime configuration has an override
108
// setting for the JDK and if it's active.
109
if (propertyMap.containsKey(CUSTOM_JDK_PROP)) {
110             String JavaDoc overrideJDK = (String JavaDoc) propertyMap.get(CUSTOM_OVERRIDE_JDK_PROP);
111             String JavaDoc customJDK = (String JavaDoc) propertyMap.get(CUSTOM_JDK_PROP);
112             if ("1".equals(overrideJDK)) {
113                 // JDK override is activated for this runtime config.
114
JDKPathSet newJDKPathSet = projectPathSet.getJDK(customJDK);
115                 if (newJDKPathSet != null) {
116                     jdkPathSet = newJDKPathSet;
117                 }
118             }
119         }
120
121         String JavaDoc jdkHomePath = jdkPathSet.getHomePath().getFileObject().toString();
122         envProvider.setProperty("JBUILDER_RUNTIME_JDK", jdkHomePath);
123
124         String JavaDoc[] otherDirs = OTHER_DIRS.getValues(propertyMap);
125         for (int index = 0; index < otherDirs.length; index++) {
126             String JavaDoc curDirEntry = otherDirs[index];
127             curDirEntry = envProvider.replaceEnvVariables(curDirEntry);
128             File JavaDoc dir = new File JavaDoc(curDirEntry);
129             /*
130                    if (dir.exists() && dir.isDirectory()) {
131               addAllDirLibsToClassPath(buffer, dir);
132                    }
133              */

134             if (buffer.length() > 0) {
135                 buffer.append(File.pathSeparatorChar);
136             }
137             buffer.append(dir.toString());
138         }
139         return buffer.toString();
140     }
141
142
143     /**
144      * Add all JARs and Zips from a directory to the classpath.
145      *
146      * @param buffer is the current classpath to add to
147      * @param directory is the location to scan
148      * @return all the
149      */

150     private void addAllDirLibsToClassPath(StringBuffer JavaDoc buffer, File JavaDoc directory) {
151         String JavaDoc classPath = buffer.toString();
152         File JavaDoc[] files = directory.listFiles();
153         for (int index = 0; index < files.length; index++) {
154             if (files[index].getName().toLowerCase().endsWith("jar") ||
155                 files[index].getName().toLowerCase().endsWith("zip")) {
156                 if (classPath.indexOf(files[index].getAbsolutePath()) == -1) {
157                     buffer.append(File.pathSeparatorChar).
158                         append(files[index].getAbsolutePath());
159                 }
160             }
161         }
162     }
163
164     /**
165      * Return the main class to run - JBuilder itself.
166      */

167     public String JavaDoc getMainClassName(JBProject project, Map JavaDoc propertyMap) {
168         return MAIN_CLASS.getValue(propertyMap);
169     }
170
171     /**
172      * Provide the parameters for JBuilder.
173      */

174     public String JavaDoc getParameters(JBProject project, Map JavaDoc propertyMap) {
175         return SysEnvPropertiesProvider.getInstance().replaceEnvVariables(JB_PARAMS.getValue(propertyMap));
176     }
177
178     /**
179      * Supply the parameters for the JVM.
180      */

181     public String JavaDoc getVMParameters(JBProject project, Map JavaDoc propertyMap) {
182         return SysEnvPropertiesProvider.getInstance().replaceEnvVariables(VM_PARAMS.getValue(propertyMap));
183     }
184 }
185
Popular Tags