001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.wizard.command.project.impl;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.util.HashSet;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import org.apache.tools.ant.DefaultLogger;
028: import org.apache.tools.ant.Project;
029: import org.apache.tools.ant.ProjectHelper;
030: import org.romaframework.core.io.virtualfile.VirtualFile;
031: import org.romaframework.core.io.virtualfile.VirtualFileFactory;
032: import org.romaframework.wizard.MainWizard;
033: import org.romaframework.wizard.ProjectManager;
034: import org.romaframework.wizard.WizardConstants;
035: import org.romaframework.wizard.command.project.OptionalProjectBaseWizardCommand;
036: import org.romaframework.wizard.helper.FileManagementHelper;
037: import org.romaframework.wizard.properties.ModuleProperties;
038: import org.romaframework.wizard.properties.ProjectProperties;
039:
040: /**
041: * This wizard add a module to the current project. Modules are in ROMA_HOME/modules. See help to know syntax.
042: *
043: * @author Luca Garulli (luca.garulli@assetdata.it)
044: */
045: public class ProjectAddModuleWizard extends
046: OptionalProjectBaseWizardCommand {
047:
048: public static final String NAME = "add";
049:
050: public String getName() {
051: return NAME;
052: }
053:
054: @Override
055: public String getParameters() {
056: return super .getParameters() + " <module-name>";
057: }
058:
059: public void execute(String[] iParameters) {
060: if (iParameters.length < 2)
061: syntaxError();
062:
063: iParameters = parseOptionalParameters(iParameters);
064:
065: // READ ARGUMENTS
066: String moduleName = iParameters[1];
067:
068: String sourcePath = MainWizard.getRomaHome() + "modules/"
069: + moduleName;
070:
071: sourcePath = FileManagementHelper.getVirtualPath(sourcePath);
072: if (sourcePath == null) {
073: getIO().getError().println(
074: "Error: module '" + moduleName
075: + "' not found in directory "
076: + MainWizard.getRomaHome() + "modules");
077: return;
078: }
079:
080: ModuleProperties moduleProperties;
081: try {
082: moduleProperties = new ModuleProperties(sourcePath);
083:
084: if (moduleProperties.existsFile()) {
085: getIO()
086: .getOutput()
087: .println(
088: "Installing module: '"
089: + moduleName
090: + "' version: "
091: + moduleProperties
092: .getProperty(ModuleProperties.VERSION_VAR)
093: + "\n");
094:
095: addDependentModules(moduleProperties);
096: }
097: } catch (IOException e1) {
098: getIO().getError().println(
099: "Error: Cannot load module's properties file "
100: + ModuleProperties.MODULE_FILE_NAME
101: + " in: " + sourcePath);
102: return;
103: }
104:
105: String projectPath = ProjectManager.getInstance()
106: .getProjectPath(projectName);
107:
108: ProjectProperties projProp = ProjectManager.getInstance()
109: .getProjectInfo(projectName);
110:
111: if (projProp == null)
112: return;
113:
114: if (!moduleName.startsWith(WizardConstants.PROJECT_TYPE_PREFIX)) {
115: // COPY SCAFFOLDING ONLY IN NO-PROJECT MODULES
116: VirtualFile scaffoldingFile = FileManagementHelper
117: .getVirtualFile(sourcePath + "/scaffolding");
118: // EXECUTE THE COPY OF SCAFFOLDING IF ANY
119: FileManagementHelper.executeCopy(getIO(), scaffoldingFile,
120: projectPath, true);
121: }
122:
123: VirtualFile sourceLibPath = FileManagementHelper
124: .getVirtualFile(sourcePath + "/lib/");
125:
126: String runTimeLibPath = projProp
127: .getProperty(ProjectProperties.RUNTIME_LIBS_VAR);
128:
129: // EXECUTE THE COPY OF LIBS IF ANY (CHECK FOR PROJECT'S PROPERTY
130: // COMPILE-TIME)
131: FileManagementHelper.executeCopy(getIO(), sourceLibPath,
132: projectPath + "/" + runTimeLibPath, true);
133:
134: String scriptPath = sourcePath + "/wizard/wizard.xml";
135: VirtualFile antFile = null;
136: try {
137: antFile = VirtualFileFactory.getFile(new URL(scriptPath));
138: } catch (MalformedURLException e) {
139: getIO().getError().println(
140: "Cannot execute script: " + scriptPath);
141: }
142:
143: ProjectProperties prjProperties = ProjectManager.getInstance()
144: .getProjectInfo(projectName);
145:
146: if (antFile != null && antFile.exists()) {
147: executeAntScript(antFile, prjProperties);
148: }
149:
150: if (moduleProperties.existsFile()) {
151: prjProperties.addInstalledModule(moduleName,
152: moduleProperties
153: .getProperty(ModuleProperties.VERSION_VAR));
154: }
155: }
156:
157: private void executeAntScript(VirtualFile antFile,
158: ProjectProperties prjProperties) {
159: File buildFile = new File(antFile.getAbsolutePath());
160: Project p = new Project();
161:
162: // BIND ALL PROJECT VARIABLES
163: p.setUserProperty("project.name", projectName);
164:
165: copyProjectProperty(p, prjProperties, "project.src",
166: ProjectProperties.SRC_VAR);
167: copyProjectProperty(p, prjProperties, "project.package",
168: ProjectProperties.PACKAGE_VAR);
169: copyProjectProperty(p, prjProperties, "project.package-path",
170: ProjectProperties.PACKAGE_PATH_VAR);
171: copyProjectProperty(p, prjProperties, "project.run-time-lib",
172: ProjectProperties.RUNTIME_LIBS_VAR);
173: copyProjectProperty(p, prjProperties,
174: "project.compile-time-lib",
175: ProjectProperties.COMPTIME_LIBS_VAR);
176: copyProjectProperty(p, prjProperties, "project.ioc-path",
177: ProjectProperties.IOC_PATH_VAR);
178: copyProjectProperty(p, prjProperties, "project.ioc-file",
179: ProjectProperties.IOC_FILE_VAR);
180:
181: p.setUserProperty("ant.file", buildFile.getAbsolutePath());
182:
183: DefaultLogger consoleLogger = new DefaultLogger();
184: consoleLogger.setErrorPrintStream(getIO().getError());
185: consoleLogger.setOutputPrintStream(getIO().getOutput());
186: consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
187: p.addBuildListener(consoleLogger);
188:
189: p.init();
190: ProjectHelper helper = ProjectHelper.getProjectHelper();
191: p.addReference("ant.projectHelper", helper);
192: helper.parse(p, buildFile);
193: p.executeTarget(p.getDefaultTarget());
194: }
195:
196: private void copyProjectProperty(Project p,
197: ProjectProperties prjProperties, String iName, String iValue) {
198: String value = prjProperties.getProperty(iValue);
199: if (value != null)
200: p.setUserProperty(iName, value);
201: }
202:
203: private void addDependentModules(ModuleProperties moduleProperties) {
204: Map<String, String> moduleDependencies = moduleProperties
205: .getDependencies();
206: if (moduleDependencies.size() == 0)
207: return;
208:
209: getIO().getOutput()
210: .println(
211: "Found dependent modules: "
212: + moduleDependencies + "\n");
213:
214: for (Map.Entry<String, String> depModule : moduleDependencies
215: .entrySet()) {
216: ProjectProperties projProp = ProjectManager.getInstance()
217: .getProjectInfo(projectName);
218: if (projProp != null
219: && projProp
220: .getProperty(ProjectProperties.MODULE_PREFIX_VAR
221: + depModule.getKey()) != null
222: && projProp.getProperty(
223: ProjectProperties.MODULE_PREFIX_VAR
224: + depModule.getKey()).equals(
225: depModule.getValue()))
226: continue; // module already added
227:
228: // TODO: deep module version test
229:
230: // ADD DEPENDENT MODULE
231: new ProjectAddModuleWizard().execute(new String[] {
232: ProjectAddModuleWizard.NAME, depModule.getKey(),
233: moduleDependencies.get(depModule.getValue()) });
234: }
235: }
236:
237: @Override
238: public void help() {
239: super .help();
240:
241: StringBuffer buffer = new StringBuffer();
242: buffer
243: .append(" <module-name> is the module name. See below to know what modules you have installed");
244: buffer.append("\n");
245: buffer
246: .append("\nPlease specify the module to add to your project between the following discovered in the $ROMA_HOME/modules directory:\n");
247:
248: // PRINT AVAILABLE MODULE LIST
249: File modulesDir = new File(MainWizard.getRomaHome() + "modules");
250: File[] modulesFiles = modulesDir.listFiles();
251: Set<String> modules = new HashSet<String>();
252: String moduleName;
253: for (File moduleDir : modulesFiles) {
254: moduleName = moduleDir.getName();
255: if (moduleName.equals(WizardConstants.CORE_MODULE_NAME))
256: continue;
257:
258: if (moduleDir.isDirectory())
259: // ADD DIRECTORY MODULE
260: modules.add(moduleName);
261: else if (moduleDir.getName().endsWith(
262: WizardConstants.MODULE_PACKAGE_EXT))
263: // ADD ZIP MODULE
264: modules.add(moduleName.substring(0, moduleName.length()
265: - WizardConstants.MODULE_PACKAGE_EXT.length()));
266: }
267:
268: for (String module : modules) {
269: buffer.append("\n- " + module);
270: }
271:
272: buffer.append("\n");
273: buffer.append("\nExample:");
274: buffer.append("\n");
275: buffer.append("\nroma " + getName() + " web-jetty");
276:
277: getIO().getError().println(buffer);
278: }
279: }
|