001: /*
002: * Copyright 2006-2007 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;
018:
019: import java.io.File;
020:
021: import org.romaframework.core.Utility;
022: import org.romaframework.core.command.CommandIO;
023: import org.romaframework.core.resource.ResourceResolver;
024: import org.romaframework.wizard.command.WizardCommand;
025: import org.romaframework.wizard.command.WizardCommandManager;
026: import org.romaframework.wizard.command.project.impl.ProjectCreateWizard;
027:
028: /**
029: * Main class executed on roma.bat and roma.sh shell command. It load the selected wizard and execute it. Wizards reside under
030: * command package.
031: *
032: * @author Luca Garulli (luca.garulli@assetdata.it)
033: */
034: public class MainWizard {
035:
036: private static CommandIO iO = new CommandIO();
037: private static final String ROMA_HOME = "roma.home";
038: private static ResourceResolver resourceResolver;
039: private static WizardResourceResolver wizard;
040:
041: public static void main(String[] iArgs) {
042: init();
043:
044: resourceResolver
045: .addClassPath("${roma.home}/wizards/wizard-main/lib/roma-*.jar");
046: resourceResolver
047: .addClassPath("${roma.home}/wizards/wizard-crud/lib/roma-*.jar");
048:
049: resourceResolver.loadResources("");
050:
051: if (iArgs.length == 0) {
052: help();
053: return;
054: }
055:
056: // EXECUTE THE BUILDER
057: WizardCommand wizardInstance = getWizardClass(iArgs[0]);
058: wizardInstance.execute(iArgs);
059: }
060:
061: private static void init() {
062: // ASSURE ROMA HOME ENVIRONMENT VARIABLE IS SETTED FIRST OF ALL
063: getRomaHome();
064:
065: wizard = new WizardResourceResolver();
066: resourceResolver = new ResourceResolver();
067: }
068:
069: private static WizardCommand getWizardClass(String iWizardName) {
070: WizardCommand wizardInstance = WizardCommandManager
071: .getInstance().getConfiguration(iWizardName);
072:
073: if (wizardInstance == null) {
074: getIO()
075: .getError()
076: .println(
077: "Wizard '"
078: + iWizardName
079: + "' not found. Type 'roma' without arguments to know installed wizards.");
080: MainWizard.stop(1);
081: }
082:
083: return wizardInstance;
084: }
085:
086: public static void help() {
087: getIO().getOutput().println(WizardConstants.HEADER);
088: getIO()
089: .getOutput()
090: .println(
091: "\nPlease specify the wizard to use between the following wizards discovered in the classpath:\n");
092:
093: for (WizardCommand wiz : WizardCommandManager.getInstance()
094: .getConfigurationValues()) {
095: getIO().getOutput().println(
096: "- " + wiz.getName() + " " + wiz.getParameters());
097: }
098: getIO().getOutput().println(
099: "\nExample: roma " + ProjectCreateWizard.NAME
100: + " web blog org.test.blog C:/temp");
101:
102: String prjName = ProjectManager.getInstance()
103: .getCurrentProjectName();
104: String prjPath = ProjectManager.getInstance().getProjectPath(
105: prjName);
106:
107: if (prjName != null) {
108: getIO().getOutput().println(
109: "\nCurrent project: <" + prjName + "> in <"
110: + prjPath + ">");
111: } else {
112: getIO()
113: .getOutput()
114: .println(
115: "\nNo current project setted. Use the 'switch' command to set it.");
116: }
117: }
118:
119: public static String getRomaHome() {
120: String home = System.getProperty(ROMA_HOME);
121:
122: if (home == null) {
123: getIO().getError().println(
124: "System property '" + ROMA_HOME + "' not setted!");
125: getIO().getError().println(
126: "Assure to run the wizard using -D" + ROMA_HOME
127: + "=<roma-path>");
128: MainWizard.stop(1);
129: }
130:
131: if (!home.endsWith(File.separator))
132: home += File.separator;
133:
134: return Utility.getUniversalResourcePath(home);
135: }
136:
137: public static CommandIO getIO() {
138: return iO;
139: }
140:
141: public static void setIO(CommandIO io) {
142: iO = io;
143: }
144:
145: public static void stop(int iResult) {
146: System.exit(iResult);
147: }
148: }
|