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;
018:
019: import java.io.FileInputStream;
020: import java.io.FileOutputStream;
021: import java.util.Properties;
022:
023: import org.romaframework.wizard.properties.ProjectProperties;
024: import org.romaframework.wizard.schema.NoDTDProperties;
025:
026: /**
027: * Handle ROMA projects. It load and save configuration to ROMA_HOME/config/projects.properties.
028: *
029: * @author Luca Garulli (luca.garulli@assetdata.it)
030: */
031: public class ProjectManager {
032: private Properties properties = null;
033: private String fileName;
034:
035: private static final ProjectManager instance = new ProjectManager();
036:
037: public static final String ROMA_FILE_NAME = "projects.xml";
038: public static final String LAST_PROJECT_VAR = "lastProject";
039: public static final String PROJECT_PATH_EXT_VAR = ".path";
040:
041: private static final String COMMENTS = "ROMA Framework configuration file";
042:
043: protected ProjectManager() {
044: }
045:
046: public synchronized String getCurrentProjectName() {
047: if (!loadFromFile())
048: return null;
049: return properties.getProperty(LAST_PROJECT_VAR);
050: }
051:
052: public synchronized void switchCurrentProject(String iProjectName,
053: String iProjectLocation) {
054: if (!loadFromFile())
055: return;
056:
057: // MainWizard.getIO().getOutput().println("\n[ROMA] Current project is: " + iProjectName);
058: // MainWizard.getIO().getOutput().println("\n[ROMA] Note: To switch project use the 'switch' command");
059:
060: properties.setProperty(LAST_PROJECT_VAR, iProjectName);
061:
062: if (iProjectLocation != null)
063: // UPDATE ALSO PROJECT PATH
064: properties.setProperty(iProjectName + PROJECT_PATH_EXT_VAR,
065: iProjectLocation);
066:
067: saveToFile();
068: }
069:
070: /**
071: * Update project information overwriting previous info.
072: *
073: * @param iProjectName
074: * Project Name
075: * @param iInfo
076: * Map<String,String> containing all info to store
077: */
078: public synchronized void setProjectPath(String iProjectName,
079: String iProjectLocation) {
080: if (!loadFromFile())
081: return;
082:
083: properties.setProperty(iProjectName + PROJECT_PATH_EXT_VAR,
084: iProjectLocation);
085:
086: saveToFile();
087: }
088:
089: /**
090: * Return the project path required
091: *
092: * @param iProjectName
093: * @return
094: */
095: public String getProjectPath(String iProjectName) {
096: if (!loadFromFile())
097: return null;
098:
099: return properties.getProperty(iProjectName
100: + PROJECT_PATH_EXT_VAR);
101: }
102:
103: /**
104: * Return the project information required.
105: *
106: * @param iProjectName
107: * Project Name
108: * @param iVarName
109: * Name of variable to get
110: * @return
111: */
112: public synchronized ProjectProperties getProjectInfo(
113: String iProjectName) throws WizardException {
114: String path = getProjectPath(iProjectName);
115: if (path == null)
116: throw new WizardException("No path found for the project "
117: + iProjectName);
118:
119: ProjectProperties prj;
120: try {
121: prj = new ProjectProperties(path);
122: return prj;
123: } catch (Exception e) {
124: MainWizard.getIO().getError().println(
125: "Error on loading project properties from project path: "
126: + path);
127: MainWizard.getIO().getError().println(
128: "Check if the project path exists.\n");
129: MainWizard.getIO().getError().println(e);
130: throw new WizardException(
131: "Error on loading project properties from project path: "
132: + path, e);
133: }
134: }
135:
136: private void saveToFile() {
137: try {
138: properties.storeToXML(new FileOutputStream(fileName),
139: COMMENTS);
140: } catch (Exception e) {
141: e.printStackTrace();
142: }
143: }
144:
145: private boolean loadFromFile() {
146: if (properties != null)
147: return true;
148:
149: properties = new NoDTDProperties();
150: fileName = MainWizard.getRomaHome() + "config/"
151: + ROMA_FILE_NAME;
152: try {
153: properties.loadFromXML(new FileInputStream(fileName));
154: } catch (Exception e) {
155: e.printStackTrace();
156: return false;
157: }
158: return true;
159: }
160:
161: public static ProjectManager getInstance() {
162: return instance;
163: }
164: }
|