001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework;
043:
044: import java.io.File;
045: import java.util.Collection;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.List;
049:
050: import org.dom4j.Document;
051: import org.dom4j.Node;
052:
053: import org.netbeans.modules.uml.core.coreapplication.ICoreProduct;
054: import org.netbeans.modules.uml.core.metamodel.core.foundation.IConfigManager;
055: import org.netbeans.modules.uml.core.support.umlsupport.IStrings;
056: import org.netbeans.modules.uml.core.support.umlsupport.ProductRetriever;
057: import org.netbeans.modules.uml.core.support.umlsupport.StringUtilities;
058: import org.netbeans.modules.uml.core.support.umlsupport.Strings;
059: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
060:
061: /**
062: * @author sumitabhk
063: *
064: */
065: public class FacilityManager implements IFacilityManager {
066: String m_ConfigurationFile;
067: String name;
068: HashMap<String, IFacility> m_FacilityMap = new HashMap<String, IFacility>();
069: HashMap<String, IFacilityManager> m_ManagerMap = new HashMap<String, IFacilityManager>();
070:
071: /* (non-Javadoc)
072: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.IFacilityManager#retrieveFacility(java.lang.String)
073: */
074: public IFacility retrieveFacility(String name) {
075: if (name == null)
076: return null;
077:
078: // If there is a '.' in the facility name then the facility is defined in a
079: // sub facility.
080: int pos = 0;
081:
082: if ((pos = name.indexOf(".")) != -1) {
083: String managerName = name.substring(0, pos);
084: String facilityName = name.substring(pos + 1);
085: return retrieveFromSubManager(managerName, facilityName);
086: } else {
087: return retrieveFromManager(name);
088: }
089: }
090:
091: /* (non-Javadoc)
092: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.IFacilityManager#getConfigurationFile()
093: */
094: public String getConfigurationFile() {
095: return m_ConfigurationFile;
096: }
097:
098: /* (non-Javadoc)
099: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.IFacilityManager#setConfigurationFile(java.lang.String)
100: */
101: public void setConfigurationFile(String value) {
102: if (value == null)
103: return;
104:
105: m_ConfigurationFile = getConfigLocation(value);
106: initializeManager(m_ConfigurationFile);
107: }
108:
109: /* (non-Javadoc)
110: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.IFacilityManager#getFacilityNames()
111: */
112: public IStrings getFacilityNames() {
113:
114: IStrings pFacilityNames = new Strings();
115: getFacilityNames(pFacilityNames);
116: return getSubFacilityNames(pFacilityNames);
117: }
118:
119: /**
120: * Retrieve the names of all the facilities in a configuration file.
121: *
122: * @param pFacilityNames [in] The collection to be filled
123: *
124: * @return
125: *
126: */
127:
128: private IStrings getFacilityNames(IStrings pFacilityNames) {
129: if (pFacilityNames == null)
130: return null;
131:
132: Document pConfigDoc = getConfigurationDocument();
133:
134: if (pConfigDoc != null) {
135: String xpath = "//Facility";
136:
137: List pNodeList = XMLManip.selectNodeList(pConfigDoc, xpath);
138:
139: if (pNodeList != null) {
140: int max = pNodeList.size();
141: for (int index = 0; index < max; index++) {
142: Node pNode = (Node) pNodeList.get(index);
143:
144: if (pNode != null) {
145: String name = XMLManip.getAttributeValue(pNode,
146: "name");
147: if (name != null)
148: pFacilityNames.add(name);
149: }
150: }
151: }
152: }
153: return pFacilityNames;
154: }
155:
156: /**
157: *
158: * Determines the location of the configuration file. If the specified
159: * filename is an absolute path to the configuration file then the
160: * supplied file name is returned. Otherwise the file is assumed to live
161: * in the home location of the perference manager.
162: *
163: * @param filename [in] The name of the configuration file
164: * @return String location
165: *
166: */
167: private String getConfigLocation(String fileName) {
168: if (fileName == null)
169: return null;
170:
171: File cfg = new File(fileName);
172:
173: if (cfg.isAbsolute())
174: return fileName;
175:
176: ICoreProduct prod = ProductRetriever.retrieveProduct();
177: if (prod != null) {
178: IConfigManager configMan = prod.getConfigManager();
179: if (configMan != null) {
180: String homeLocation = configMan
181: .getDefaultConfigLocation();
182:
183: if (homeLocation != null
184: && homeLocation.trim().length() > 0) {
185: File home = new File(homeLocation, fileName);
186: return home.getAbsolutePath();
187: }
188: }
189: }
190: return null;
191: }
192:
193: /**
194: * Retrieves an instance of the facility. The CLSID that is to be created
195: * is specified in the XML config file. The <I>progid</I> attribute specifies
196: * CoClass to create.
197: *
198: * @param name [in] The name of facility
199: * @param pFacilityNode [in] The XML node that contains the facility information
200: * @param pFacility [in] The facility to update
201: */
202: private IFacility getFacilityInstance(String name, Node facilityNode) {
203: if (facilityNode == null)
204: return null;
205:
206: // Create the specified facility. The CoClass to create is specified in the Facility
207: // XML configuration file.
208: String classname = XMLManip.getAttributeValue(facilityNode,
209: "progid");
210: try {
211: return (IFacility) Class.forName(classname).newInstance();
212: } catch (Exception e) {
213: e.printStackTrace();
214: }
215: return null;
216: }
217:
218: /**
219: * Retrieves the XML Document that is the Facility configuration.
220: *
221: * @param pVal [out] The configuraton document
222: */
223: private Document getConfigurationDocument() {
224: return XMLManip.getDOMDocument(getConfigurationFile());
225: }
226:
227: /**
228: * Initializes a facility with it's description. The description
229: * is retrieved from an XML configuration file.
230: *
231: * @param manip [in] XML helper
232: * @param pFacilityNode [in] The XML node that contains the facility information
233: * @param pFacility [in] The facility to update
234: */
235: private IFacility getDescription(Node pFacilityNode,
236: IFacility pFacility) {
237: if (pFacility == null || pFacilityNode == null)
238: return null;
239:
240: String desc = XMLManip.retrieveNodeTextValue(pFacilityNode,
241: "Description");
242: if (desc != null)
243: pFacility.setDescription(desc);
244: return pFacility;
245: }
246:
247: /**
248: * Initializes a facility with it's properties. The properties information
249: * is retrieved from a XML configuration file.
250: *
251: * @param manip [in] XML helper
252: * @param pFacilityNode [in] The XML node that contains the facility information
253: * @param pFacility [in] The facility to update
254: */
255: private void getProperties(Node pFacilityNode, IFacility pFacility) {
256: if (pFacilityNode == null || pFacility == null)
257: return;
258:
259: String xpath = "Properties/Property";
260: List pNodeList = XMLManip.selectNodeList(pFacilityNode, xpath);
261:
262: if (pNodeList != null) {
263: int max = pNodeList.size();
264: for (int i = 0; i < max; i++) {
265: Node pNode = (Node) pNodeList.get(i);
266:
267: if (pNode != null) {
268: String propertyName = XMLManip.getAttributeValue(
269: pNode, "name");
270: String propertyValue = XMLManip.getAttributeValue(
271: pNode, "value");
272: pFacility.addProperty(propertyName, propertyValue);
273: }
274: }
275: }
276: }
277:
278: /**
279: * Creates and initializes an instance of a facility. The facility details are
280: * retrieved from a XML file.
281: *
282: * @param name [in] The name of the facility
283: * @param pFacilityNode [in] The XML node that contains the facility information
284: * @param pVal [out] The created facility
285: */
286: private IFacility retrieveFacilityDetails(String name,
287: Node pFacilityNode) {
288: if (pFacilityNode == null)
289: return null;
290:
291: IFacility pNewFac = getFacilityInstance(name, pFacilityNode);
292: getProperties(pFacilityNode, pNewFac);
293: getDescription(pFacilityNode, pNewFac);
294: return pNewFac;
295: }
296:
297: /**
298: * Loads all of the sub facilities manager that are defined in a
299: * facility configuration file.
300: *
301: * @param pConfigFile [in] The facility manager config XML document
302: */
303:
304: private void loadSubFacilityManagers(Document pConfigFile) {
305: if (pConfigFile == null)
306: return;
307:
308: List pNodeList = XMLManip.selectNodeList(pConfigFile,
309: "//FacilityManager");
310:
311: if (pNodeList != null) {
312: int max = pNodeList.size();
313: for (int i = 0; i < max; i++) {
314: Node pNode = (Node) pNodeList.get(i);
315: if (pNode != null) {
316: String managerName = XMLManip.getAttributeValue(
317: pNode, "name");
318: String managerConfigFile = XMLManip
319: .getAttributeValue(pNode, "config");
320:
321: IFacilityManager pManager = new FacilityManager();
322: pManager.setConfigurationFile(managerConfigFile);
323: pManager.setName(managerName);
324: m_ManagerMap.put(managerName, pManager);
325: }
326: }
327: }
328: }
329:
330: /**
331: * Retrieves the specified facility from a sub facility manager. When the name contains
332: * a <i>'.'</i> character then the facility is retrieve from a child manager.
333: *
334: * @param managerName [in] The name of the child manager
335: * @param facilityName [in] The name of the facility
336: * @param pVal [out] The facility
337: */
338: private IFacility retrieveFromSubManager(String managerName,
339: String facilityName) {
340: if (managerName == null || facilityName == null)
341: return null;
342:
343: IFacilityManager pManager = (IFacilityManager) m_ManagerMap
344: .get(managerName);
345:
346: if (pManager != null) {
347: return pManager.retrieveFacility(facilityName);
348: } else
349: return null;
350: }
351:
352: /**
353: * Checks if the facility is specified as a singleton.
354: *
355: * @param pFacilityNode [in] The XML node that contains the facility information
356: * @return true if the facility is a singleton, false otherwise.
357: */
358: private boolean isSingleton(Node pFacilityNode) {
359: if (pFacilityNode == null)
360: return false;
361:
362: return Boolean.valueOf(
363: XMLManip.getAttributeValue(pFacilityNode, "singleton"))
364: .booleanValue();
365: }
366:
367: /**
368: * Checks in the map of facilities that have already been loaded.
369: *
370: * @param name [in] The name of the facility
371: * @param pVal [out] The facility
372: */
373: private IFacility lookupFacility(String name) {
374: if (name == null)
375: return null;
376:
377: return m_FacilityMap.get(name);
378: }
379:
380: /**
381: * Retrieves the specified facility from the manager. If the facility has already
382: * be initialized then retrieve it. If the faciltiy has not already been initialized,
383: * create an instance of the facility and initialize the properties.
384: *
385: * @param name [in] The name of the facility
386: * @param pVal [out] The facility
387: */
388: private IFacility retrieveFromManager(String name) {
389: IFacility pFacility = lookupFacility(name);
390:
391: // If the lookup failed to retrieve the facility then find the facility
392: // in the configuration file and retrieve its details.
393: if (pFacility == null) {
394: Document pConfigDoc = getConfigurationDocument();
395:
396: if (pConfigDoc != null) {
397: String xpath = "//Facility[@name=\"" + name + "\"]";
398:
399: Node pNode = XMLManip.selectSingleNode(pConfigDoc,
400: xpath);
401:
402: if (pNode != null) {
403: pFacility = retrieveFacilityDetails(name, pNode);
404:
405: if (pFacility != null && isSingleton(pNode) == true) {
406: m_FacilityMap.put(name, pFacility);
407: }
408: }
409: }
410: }
411: return pFacility;
412: }
413:
414: /**
415: *
416: * Populates this manager with the contents of the given
417: * configuration file
418: *
419: * @return HRESULT
420: *
421: */
422: private void initializeManager(String configLoc) {
423: if (configLoc != null) {
424: Document pDoc = XMLManip
425: .getDOMDocument(m_ConfigurationFile);
426:
427: if (pDoc != null) {
428: loadSubFacilityManagers(pDoc);
429: }
430: }
431: }
432:
433: /**
434: * Retrieves the names of the subfacilities defined in a configuration
435: * file.
436: *
437: * @param pFacilityNames[in] The names of the facility contexts
438: */
439:
440: private IStrings getSubFacilityNames(IStrings pFacilityNames) {
441: if (pFacilityNames == null)
442: return null;
443:
444: Collection col = m_ManagerMap.values();
445: if (col != null) {
446: Iterator iter = col.iterator();
447: IFacilityManager pManager;
448: String separator = ".";
449: while (iter.hasNext()) {
450: pManager = (IFacilityManager) iter.next();
451: if (pManager != null) {
452: IStrings pSubFacilityNames = pManager
453: .getFacilityNames();
454: String managerName = pManager.getName();
455: if (pSubFacilityNames != null) {
456: int count = pSubFacilityNames.getCount();
457: for (int index = 0; index < count; index++) {
458: String name = pSubFacilityNames.item(index);
459:
460: if (name != null) {
461: String facilityName = managerName;
462: facilityName += separator;
463: facilityName += name;
464:
465: pFacilityNames.add(facilityName);
466: }
467: }
468: }
469: }
470: }
471: }
472: return pFacilityNames;
473: }
474:
475: public void setName(String nm) {
476: name = nm;
477: }
478:
479: public String getName() {
480: return name;
481: }
482: }
|