001: package com.xoetrope.carousel.services;
002:
003: import java.io.File;
004: import java.io.FileNotFoundException;
005: import java.io.FileReader;
006: import java.io.FileWriter;
007:
008: import net.xoetrope.editor.project.XEditorProject;
009: import net.xoetrope.editor.project.XEditorProjectProperty;
010: import net.xoetrope.optional.data.XOptionalDataSource;
011: import net.xoetrope.xml.XmlElement;
012: import net.xoetrope.xml.nanoxml.NanoXmlParser;
013: import net.xoetrope.xui.data.XBaseModel;
014: import net.xoetrope.data.XDataSource;
015: import net.xoetrope.debug.DebugLogger;
016: import net.xoetrope.xui.data.XModelHelper;
017: import net.xoetrope.xui.helper.Constants;
018:
019: /**
020: * Singleton class for tracking loaded services and changes being made in the
021: * editor to the services. The services are stored as XmlElements within the
022: * services Hashtable. This storage mechanism makes it easier to load and save
023: * data
024: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
025: * the GNU Public License (GPL), please see license.txt for more details. If
026: * you make commercial use of this software you must purchase a commercial
027: * license from Xoetrope.</p>
028: * <p> $Revision: 1.8 $</p>
029: */
030: public class ServiceManager {
031: private XEditorProject currentProject;
032: private XBaseModel clientServicesMdl, serverServicesMdl;
033: public final static int SIDE_CLIENT = 1;
034: public final static int SIDE_SERVER = 2;
035:
036: /**
037: * Create the services Hashtable
038: */
039: public ServiceManager(XEditorProject proj) {
040: currentProject = proj;
041:
042: if (currentProject.getObject(Constants.SERVER_DATASETS) == null) {
043: DebugLogger
044: .logError("You must define a value for the server side routes and service configuration in the startup properties to use XUI's routes and servives");
045: currentProject
046: .fixError(
047: getClass(),
048: "com.xoetrope.carousel.debug.selfhealing.ServerPropertiesMissing",
049: this , null);
050: }
051:
052: loadServicesFromFile(currentProject);
053: }
054:
055: /**
056: * Get the sevices file name from the XLibXModelManager and load it into an
057: * xml element. Iterate the child elements and add them to the services
058: * Hashtable.
059: *
060: * @param currentProject the currentProject we are working with
061: */
062: public void loadServicesFromFile(XEditorProject proj) {
063: currentProject = proj;
064:
065: if (clientServicesMdl == null) {
066: clientServicesMdl = new XBaseModel();
067: clientServicesMdl.setTagName(Constants.SERVICES);
068: }
069:
070: if (serverServicesMdl == null) {
071: serverServicesMdl = new XBaseModel();
072: serverServicesMdl.setTagName(Constants.SERVICES);
073: }
074:
075: String servicesFilename = (String) ((XEditorProjectProperty) currentProject
076: .getObject(Constants.SERVICES)).property;
077: loadServiceFile(clientServicesMdl, servicesFilename,
078: "resources");
079:
080: servicesFilename = (String) ((XEditorProjectProperty) currentProject
081: .getObject(Constants.SERVER_SERVICES)).property;
082: loadServiceFile(serverServicesMdl, servicesFilename,
083: "resources" + File.separator + "server");
084: }
085:
086: private void loadServiceFile(XBaseModel targetMdl,
087: String servicesFilename, String resPath) {
088: if (servicesFilename != null) {
089: servicesFilename = currentProject.getPath()
090: + File.separator + resPath + File.separator
091: + servicesFilename;
092: XOptionalDataSource xlibDS = new XOptionalDataSource(
093: currentProject);
094:
095: try {
096: FileReader fr = new FileReader(servicesFilename);
097: NanoXmlParser parser = new NanoXmlParser();
098: XmlElement ele = parser.parse(fr);
099: if (ele != null)
100: xlibDS.loadTable(ele, targetMdl);
101: } catch (FileNotFoundException ex) {
102: }
103: }
104: }
105:
106: public String getRouteName(String serviceName, int side) {
107: XBaseModel serviceMdl = (XBaseModel) getServices(side).get(
108: serviceName);
109: return XModelHelper.getAttrib(serviceMdl, "route");
110: }
111:
112: public void setRouteName(String serviceName, String routeName,
113: int side) {
114: XBaseModel serviceMdl = (XBaseModel) getServices(side).get(
115: serviceName);
116: XModelHelper.setAttrib(serviceMdl, "route", routeName);
117: }
118:
119: public void removeService(String serviceName, int side) {
120: XBaseModel targetMdl = getServices(side);
121: targetMdl.removeChild(serviceName);
122: }
123:
124: /**
125: * Get a reference to the services Hashtable.
126: * @return the services Hashtable
127: */
128: public XBaseModel getServices(int side) {
129: return (side == SIDE_CLIENT ? clientServicesMdl
130: : serverServicesMdl);
131: }
132:
133: /**
134: * Save the services file using the filename passed. Iterate the services
135: * Hashtable and build up the xml hierarchy. Save the file using the root
136: * node.
137: *
138: * @param currentProject the currentProject we are working with
139: * @param filename the name of the services file
140: */
141: public void saveServices(XEditorProject project) {
142: String servicesFilename = (String) ((XEditorProjectProperty) project
143: .getObject(Constants.SERVICES)).property;
144: servicesFilename = project.getPath() + File.separator
145: + "resources" + File.separator + servicesFilename;
146: saveServiceFile(servicesFilename, clientServicesMdl);
147:
148: servicesFilename = (String) ((XEditorProjectProperty) project
149: .getObject(Constants.SERVER_SERVICES)).property;
150: servicesFilename = project.getPath() + File.separator
151: + "resources" + File.separator + "server"
152: + File.separator + servicesFilename;
153: saveServiceFile(servicesFilename, serverServicesMdl);
154: }
155:
156: private void saveServiceFile(String path, XBaseModel targetMdl) {
157: try {
158: FileWriter fw = new FileWriter(path);
159: XDataSource.outputModel(fw, targetMdl);
160: fw.flush();
161: fw.close();
162: } catch (Exception e) {
163: e.printStackTrace();
164: }
165: }
166:
167: private ModelManager getModelManager() {
168: RouteManager routeMgr = (RouteManager) currentProject
169: .getObject(ProjectRouteManager.ROUTE_MGR_LOOKUP);
170: return routeMgr.getModelMgr();
171: }
172:
173: /**
174: * Add a service with the specified name to the Hashtable. The object added is
175: * an XmlElement.
176: * @param name
177: */
178: public void addService(String name, int side) {
179: XBaseModel newService = new XBaseModel(getServices(side), name,
180: null);
181: newService.setTagName("service");
182: }
183: }
|