001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.services.codegeneration.serviceimplementationgenerator.xmldevsimulator;
016:
017: import java.io.File;
018: import java.io.FileWriter;
019: import java.io.IOException;
020: import java.io.PrintWriter;
021: import java.util.Collection;
022: import java.util.Iterator;
023:
024: import javax.naming.Context;
025: import javax.naming.InitialContext;
026: import javax.naming.NamingException;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import com.metaboss.enterprise.bo.BOException;
032: import com.metaboss.enterprise.bs.BSDomainObjectInvocationException;
033: import com.metaboss.enterprise.bs.BSException;
034: import com.metaboss.enterprise.bs.BSNamingAndDirectoryServiceInvocationException;
035: import com.metaboss.sdlctools.models.ModelRepository;
036: import com.metaboss.sdlctools.models.metabossmodel.MetaBossModelPackage;
037: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Enterprise;
038: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Operation;
039: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Service;
040: import com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.Servicemodule;
041: import com.metaboss.sdlctools.services.codegeneration.BSServiceImplementationGenerator;
042: import com.metaboss.sdlctools.services.codegeneration.CodeGenerationStylesheetAccessor;
043: import com.metaboss.sdlctools.services.codegeneration.CopyrightUtils;
044: import com.metaboss.sdlctools.services.codegenerationstylesheet.STDocumentationStylesheet;
045: import com.metaboss.sdlctools.services.codegenerationstylesheet.STEnterpriseStylesheet;
046: import com.metaboss.sdlctools.services.codegenerationstylesheet.STOperationStylesheet;
047: import com.metaboss.sdlctools.services.codegenerationstylesheet.STServiceStylesheet;
048: import com.metaboss.sdlctools.services.codegenerationstylesheet.STServicemoduleStylesheet;
049: import com.metaboss.sdlctools.services.codegenerationstylesheet.STSystemStylesheet;
050: import com.metaboss.util.DirectoryUtils;
051: import com.metaboss.util.StringUtils;
052:
053: /** Generator of developer's service simulator utilising XML files */
054: public class BSServiceImplementationGeneratorImpl implements
055: BSServiceImplementationGenerator {
056: // Commons Logging instance.
057: private static final Log sLogger = LogFactory
058: .getLog(BSServiceImplementationGeneratorImpl.class);
059:
060: public BSServiceImplementationGeneratorImpl(
061: java.util.Hashtable pEnvironment) {
062: }
063:
064: // Generates servicemodules distribution implementation sourcecode for the particular servicemodule within particular enterprise
065: public void generateSourceCodeForService(
066: String pGenerationDirectoryPath, String pServiceRef)
067: throws BSException {
068: try {
069: sLogger
070: .info("Generating xml based development simulator implementation source for "
071: + pServiceRef + " service...");
072: // Get the referenced objects from the model
073: Context lContext = new InitialContext();
074: ModelRepository lModelRepository = (ModelRepository) lContext
075: .lookup(ModelRepository.COMPONENT_URL);
076: MetaBossModelPackage lMetaBossModelPackage = (MetaBossModelPackage) lModelRepository
077: .getDefaultModelExtent();
078: Service lService = (Service) lMetaBossModelPackage
079: .getModelElement().getByRef(pServiceRef);
080: // Decide on directory name
081: String lImplementationPackageName = getPackageNameForService(lService
082: .getRef());
083: String lImplementationSourceDirectoryPath = pGenerationDirectoryPath
084: + File.separator
085: + StringUtils.replace(lImplementationPackageName,
086: ".", File.separator);
087: DirectoryUtils
088: .ensureThereIsDirectory(lImplementationSourceDirectoryPath);
089: // Generate bits and pieces
090: generateServiceImplementation(lImplementationPackageName,
091: lImplementationSourceDirectoryPath, lService);
092: generateFactoryImplementationSource(
093: lImplementationPackageName,
094: lImplementationSourceDirectoryPath, lService);
095: // Done
096: sLogger
097: .info("Done generating xml based development simulator implementation source for "
098: + pServiceRef + " service.");
099: } catch (NamingException e) {
100: throw new BSNamingAndDirectoryServiceInvocationException(e);
101: } catch (BOException e) {
102: throw new BSDomainObjectInvocationException(e);
103: }
104: }
105:
106: // Helper. Generates adapter implementation
107: private void generateServiceImplementation(
108: String pImplementationPackage, String pImplementationDir,
109: Service pService) throws BOException, BSException {
110: STDocumentationStylesheet lDocumentationStylesheet = CodeGenerationStylesheetAccessor
111: .getDocumentationStylesheet();
112: STServiceStylesheet lServiceStylesheet = CodeGenerationStylesheetAccessor
113: .getServiceStylesheet(pService);
114: Servicemodule lServicemodule = pService.getServicemodule();
115: STServicemoduleStylesheet lServicemoduleStylesheet = CodeGenerationStylesheetAccessor
116: .getServicemoduleStylesheet(lServicemodule);
117: com.metaboss.sdlctools.models.metabossmodel.enterprisemodel.System lSystem = lServicemodule
118: .getSystem();
119: STSystemStylesheet lSystemStylesheet = CodeGenerationStylesheetAccessor
120: .getSystemStylesheet(lSystem);
121: Enterprise lEnterprise = lSystem.getEnterprise();
122: STEnterpriseStylesheet lEnterpriseStylesheet = CodeGenerationStylesheetAccessor
123: .getEnterpriseStylesheet(lEnterprise);
124:
125: String lServiceOperationsFileNameBase = null;
126: {
127: StringBuffer lTempBuffer = new StringBuffer();
128: lTempBuffer.append(lEnterpriseStylesheet
129: .getNormalisedName());
130: lTempBuffer.append(".");
131: lTempBuffer.append(lSystemStylesheet.getNormalisedName());
132: lTempBuffer.append(".");
133: lTempBuffer.append(lServicemoduleStylesheet
134: .getNormalisedName());
135: lTempBuffer.append(".");
136: lTempBuffer.append(lServiceStylesheet.getNormalisedName());
137: lTempBuffer.append(".");
138: lServiceOperationsFileNameBase = lTempBuffer.toString();
139: }
140:
141: String lXMLStringsServiceAdapterFullName = lServicemoduleStylesheet
142: .getServicesGenericXmlStringsAdapterPackageName()
143: + "." + lServiceStylesheet.getAdapterClassName();
144: String lServiceImplementationFullName = pImplementationPackage
145: + "." + lServiceStylesheet.getImplementationClassName();
146: String lServiceImplementationFileName = pImplementationDir
147: + File.separator
148: + lServiceStylesheet.getImplementationClassName()
149: + ".java";
150: Collection lOperations = pService.getOperations();
151: java.io.FileWriter lFileWriter = null;
152: java.io.PrintWriter lWriter = null;
153: try {
154: lWriter = new java.io.PrintWriter(
155: lFileWriter = new java.io.FileWriter(
156: lServiceImplementationFileName));
157: CopyrightUtils.writeCopyrightToGeneratedJavaSource(lWriter,
158: getClass());
159: lWriter.println("package " + pImplementationPackage + ";");
160: lWriter.println("");
161: lWriter.println("import java.io.File;");
162: lWriter.println("import java.io.FileFilter;");
163: lWriter.println("import java.io.FileInputStream;");
164: lWriter.println("import java.io.IOException;");
165: lWriter.println("import java.io.StringReader;");
166: lWriter.println("import java.io.StringWriter;");
167: lWriter.println("import java.util.Arrays;");
168: lWriter.println("import java.util.Comparator;");
169: lWriter.println("import org.xml.sax.SAXException;");
170: lWriter.println("import javax.xml.transform.OutputKeys;");
171: lWriter.println("import javax.xml.transform.Transformer;");
172: lWriter
173: .println("import javax.xml.transform.TransformerFactory;");
174: lWriter
175: .println("import javax.xml.transform.TransformerException;");
176: lWriter
177: .println("import javax.xml.transform.TransformerConfigurationException;");
178: lWriter
179: .println("import javax.xml.transform.stream.StreamResult;");
180: lWriter
181: .println("import javax.xml.transform.stream.StreamSource;");
182: lWriter.println("import org.apache.commons.logging.Log;");
183: lWriter
184: .println("import org.apache.commons.logging.LogFactory;");
185: lWriter.println("import com.metaboss.util.StringUtils;");
186: lWriter
187: .println("import com.metaboss.enterprise.bs.BSException;");
188: lWriter
189: .println("import com.metaboss.enterprise.bs.BSUndefinedSystemPropertyException;");
190: lWriter
191: .println("import com.metaboss.enterprise.bs.BSUnexpectedProgramConditionException;");
192: lWriter
193: .println("import com.metaboss.enterprise.bs.BSUnexpectedTransactionStatusException;");
194: lWriter
195: .println("import com.metaboss.enterprise.bs.BSNamingAndDirectoryServiceInvocationException;");
196: lWriter
197: .println("import com.metaboss.enterprise.bs.BSTransactionServiceInvocationException;");
198: lWriter
199: .println("import com.metaboss.enterprise.bs.BSTransactionRolledBackException;");
200: lWriter
201: .println("import com.metaboss.enterprise.datatypes.DataTypeValidationException;");
202: lWriter.println();
203: lWriter
204: .println("/** This class contains a xml base development simulator implementation generator.");
205: lWriter
206: .println(" * This implementation is used to serve dummy responses to requests.");
207: lWriter
208: .println(" * Having received the request, dev simulator will lookup xml or xsl file in the specified directory.");
209: lWriter
210: .println(" * The name of this file will have to be the same as OperationRef and extension must be xml or xsl");
211: lWriter
212: .println(" * If xsl file is found, its contents will be used as a template to translate input document. The result");
213: lWriter
214: .println(" * of transformation will be packaged in the response.");
215: lWriter
216: .println(" * If xml file is found (second preference)- its contents will be packaged into the response without any preprocessing.");
217: lWriter
218: .println(" * <p> Note that the documents used in this implementation are governed by schema generated in the generic dom adapter.");
219: lWriter
220: .println(" * generic xmlstrings adapter and genericdomadapter translators are used to translate data between input and output structures and.");
221: lWriter.println(" * xml files.</p> */");
222: lWriter.println("public class "
223: + lServiceStylesheet.getImplementationClassName()
224: + " implements "
225: + lServiceStylesheet.getInterfaceFullName());
226: lWriter.println("{");
227: lWriter.println();
228: lWriter
229: .println(" private static final Log sLogger = LogFactory.getLog("
230: + lServiceStylesheet
231: .getImplementationClassName()
232: + ".class);");
233: lWriter
234: .println(" private static final String sDataDirectoryPathEnvironmentPropertyName = \""
235: + pImplementationPackage
236: + ".DataDirectoryPath\";");
237: lWriter
238: .println(" private File mTestDataDirectory = null;");
239: lWriter
240: .println(" private TransformerFactory mTransformerFactory = null;");
241: lWriter.println();
242: lWriter
243: .println(" /** Constructor. Has package visibility. Instances of this class must be created");
244: lWriter
245: .println(" * via object factory class located in the same package */");
246: lWriter
247: .println(" "
248: + lServiceStylesheet
249: .getImplementationClassName()
250: + "(java.util.Hashtable pEnvironment) throws Exception");
251: lWriter.println(" {");
252: lWriter
253: .println(" String lTestDataDirectoryPath = null;");
254: lWriter
255: .println(" if (pEnvironment.containsKey(sDataDirectoryPathEnvironmentPropertyName))");
256: lWriter
257: .println(" lTestDataDirectoryPath = (String)pEnvironment.get(sDataDirectoryPathEnvironmentPropertyName);");
258: lWriter.println(" else");
259: lWriter
260: .println(" if (System.getProperty(sDataDirectoryPathEnvironmentPropertyName) != null)");
261: lWriter
262: .println(" lTestDataDirectoryPath = System.getProperty(sDataDirectoryPathEnvironmentPropertyName);");
263: lWriter.println(" else");
264: lWriter
265: .println(" throw new BSUndefinedSystemPropertyException(\"Property \" + sDataDirectoryPathEnvironmentPropertyName + \" is not defined in the context or in the system properties\");");
266: lWriter
267: .println(" mTestDataDirectory = new File(lTestDataDirectoryPath).getAbsoluteFile();");
268: lWriter
269: .println(" if (!mTestDataDirectory.exists())");
270: lWriter
271: .println(" sLogger.error(\"Property \" + sDataDirectoryPathEnvironmentPropertyName + \" is defined, but directory it is pointing to does not exists. Directory: \" + mTestDataDirectory.getAbsolutePath());");
272: lWriter.println(" else");
273: lWriter
274: .println(" if (!mTestDataDirectory.canRead())");
275: lWriter
276: .println(" sLogger.error(\"Property \" + sDataDirectoryPathEnvironmentPropertyName + \" is defined, but directory it is pointing to is unreadable. Directory: \" + mTestDataDirectory.getAbsolutePath());");
277: lWriter.println(" else");
278: lWriter
279: .println(" sLogger.info(\"Will look for simulated data files in \" + mTestDataDirectory.getAbsolutePath() + \" directory.\");");
280: // Also create transformer factory
281: lWriter
282: .println(" mTransformerFactory = TransformerFactory.newInstance();");
283: lWriter.println(" }");
284: for (Iterator lOperationsIterator = lOperations.iterator(); lOperationsIterator
285: .hasNext();) {
286: Operation lOperation = (Operation) lOperationsIterator
287: .next();
288: STOperationStylesheet lOperationStylesheet = CodeGenerationStylesheetAccessor
289: .getOperationStylesheet(lOperation);
290: StringBuffer lOperationFileNameBase = new StringBuffer(
291: lServiceOperationsFileNameBase);
292: lOperationFileNameBase.append(lOperationStylesheet
293: .getNormalisedName());
294:
295: lWriter.println();
296: // Variable to keep last used file name for the particular operation
297: lWriter.println(" private File mLastDataFileFor"
298: + lOperation.getName() + " = null;");
299: lWriter.println();
300: lWriter
301: .println(" /** This method wraps around {@link "
302: + lServiceStylesheet
303: .getInterfaceFullName()
304: + "#"
305: + lOperationStylesheet.getMethodName()
306: + "("
307: + lOperationStylesheet
308: .getInputClassFullName()
309: + ") "
310: + lOperationStylesheet.getMethodName()
311: + "} method and implements");
312: lWriter
313: .println(" * xml file based functionality, which generates operation results */");
314: lWriter.println(" public "
315: + lOperationStylesheet.getResultClassFullName()
316: + " " + lOperationStylesheet.getMethodName()
317: + "( "
318: + lOperationStylesheet.getInputClassFullName()
319: + " pInput) throws BSException");
320: lWriter.println(" {");
321: lWriter.println(" if ((mLastDataFileFor"
322: + lOperation.getName()
323: + " = getNextDataFile(mLastDataFileFor"
324: + lOperation.getName() + ",\""
325: + lOperationFileNameBase.toString()
326: + "\")) == null)");
327: lWriter.println(" {");
328: lWriter
329: .println(" sLogger.info(\"Simulating "
330: + lOperation.getRef()
331: + " operation invocation. Returning empty result due to data file absence.\");");
332: lWriter.println(" return new "
333: + lOperationStylesheet.getResultClassFullName()
334: + "();");
335: lWriter.println(" }");
336: lWriter.println(" try");
337: lWriter.println(" {");
338: // Got the file.
339: // If it is an XML file, it can just be converted with adapter and used
340: lWriter
341: .println(" if (mLastDataFileFor"
342: + lOperation.getName()
343: + ".getName().toLowerCase().endsWith(\".xml\"))");
344: lWriter.println(" {");
345: lWriter
346: .println(" sLogger.info(\"Simulating "
347: + lOperation.getRef()
348: + " operation invocation. Returning result from \" + mLastDataFileFor"
349: + lOperation.getName()
350: + ".getName() + \" file.\");");
351: lWriter
352: .println(" return "
353: + lXMLStringsServiceAdapterFullName
354: + "."
355: + lOperationStylesheet
356: .getResultProxyTranslatorInnerClassName()
357: + ".fromXMLString(StringUtils.readTextStreamFully(new FileInputStream(mLastDataFileFor"
358: + lOperation.getName() + ")));");
359: lWriter.println(" }");
360: // If it is an XSL file, it can be translated and used
361: lWriter.println(" else");
362: lWriter
363: .println(" if (mLastDataFileFor"
364: + lOperation.getName()
365: + ".getName().toLowerCase().endsWith(\".xsl\"))");
366: lWriter.println(" {");
367: lWriter
368: .println(" sLogger.info(\"Simulating "
369: + lOperation.getRef()
370: + " operation invocation. Returning result from \" + mLastDataFileFor"
371: + lOperation.getName()
372: + ".getName() + \" file.\");");
373: // Perform transformation from file to string buffer than invoke adapter
374: lWriter
375: .println(" String lInputString = "
376: + lXMLStringsServiceAdapterFullName
377: + "."
378: + lOperationStylesheet
379: .getInputProxyTranslatorInnerClassName()
380: + ".toXMLString(pInput);");
381: lWriter
382: .println(" StringWriter lOutputWriter = new StringWriter();");
383: lWriter
384: .println(" Transformer lTransformer = mTransformerFactory.newTransformer(new StreamSource(mLastDataFileFor"
385: + lOperation.getName() + "));");
386: lWriter
387: .println(" lTransformer.setOutputProperty(OutputKeys.METHOD,\"xml\");");
388: lWriter
389: .println(" lTransformer.setOutputProperty(OutputKeys.STANDALONE,\"yes\");");
390: lWriter
391: .println(" lTransformer.setOutputProperty(OutputKeys.INDENT,\"no\");");
392: lWriter
393: .println(" lTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,\"no\");");
394: lWriter
395: .println(" lTransformer.transform(new StreamSource(new StringReader(lInputString)), new StreamResult(lOutputWriter));");
396: lWriter
397: .println(" return "
398: + lXMLStringsServiceAdapterFullName
399: + "."
400: + lOperationStylesheet
401: .getResultProxyTranslatorInnerClassName()
402: + ".fromXMLString(lOutputWriter.toString());");
403: lWriter.println(" }");
404: lWriter.println(" }");
405: lWriter
406: .println(" catch(DataTypeValidationException e)");
407: lWriter.println(" {");
408: lWriter
409: .println(" sLogger.error(\"Data validation error while processing \" + mLastDataFileFor"
410: + lOperation.getName()
411: + ".getName() + \" file.\", e);");
412: lWriter.println(" }");
413: lWriter.println(" catch(SAXException e)");
414: lWriter.println(" {");
415: lWriter
416: .println(" sLogger.error(\"XML parsing error while processing \" + mLastDataFileFor"
417: + lOperation.getName()
418: + ".getName() + \" file.\", e);");
419: lWriter.println(" }");
420: lWriter
421: .println(" catch(TransformerConfigurationException e)");
422: lWriter.println(" {");
423: lWriter
424: .println(" sLogger.error(\"Template transformer creation error while processing \" + mLastDataFileFor"
425: + lOperation.getName()
426: + ".getName() + \" file.\", e);");
427: lWriter.println(" }");
428: lWriter
429: .println(" catch(TransformerException e)");
430: lWriter.println(" {");
431: lWriter
432: .println(" sLogger.error(\"Template transformation error while processing \" + mLastDataFileFor"
433: + lOperation.getName()
434: + ".getName() + \" file.\", e);");
435: lWriter.println(" }");
436: lWriter.println(" catch(IOException e)");
437: lWriter.println(" {");
438: lWriter
439: .println(" sLogger.error(\"Error reading \" + mLastDataFileFor"
440: + lOperation.getName()
441: + ".getName() + \" file.\", e);");
442: lWriter.println(" }");
443: lWriter.println(" return new "
444: + lOperationStylesheet.getResultClassFullName()
445: + "();");
446: lWriter.println(" }");
447: }
448: // Commoonly used comparator, new once only
449: lWriter.println();
450: lWriter
451: .println(" private static Comparator sFileComparator = new Comparator()");
452: lWriter.println(" {");
453: lWriter
454: .println(" public int compare(Object o1, Object o2)");
455: lWriter.println(" {");
456: lWriter
457: .println(" return ((File)o1).getName().compareTo(((File)o2).getName());");
458: lWriter.println(" }");
459: lWriter.println(" };");
460:
461: // Helper class used to load next data file for the operation
462: lWriter
463: .println(" private File getNextDataFile( File pPreviousDataFile, final String pOperationFileNameBase) throws BSException");
464: lWriter.println(" {");
465: lWriter.println(" File[] lDataFiles = null;");
466: lWriter
467: .println(" if (mTestDataDirectory.exists() && mTestDataDirectory.canRead())");
468: lWriter.println(" {");
469: lWriter
470: .println(" lDataFiles = mTestDataDirectory.listFiles(new FileFilter()");
471: lWriter.println(" {");
472: lWriter
473: .println(" public boolean accept(File pPathname)");
474: lWriter.println(" {");
475: lWriter
476: .println(" if (!pPathname.isFile())");
477: lWriter.println(" return false;");
478: lWriter
479: .println(" if (pPathname.isHidden())");
480: lWriter.println(" return false;");
481: lWriter
482: .println(" if (!pPathname.canRead())");
483: lWriter.println(" return false;");
484: lWriter
485: .println(" String lName = pPathname.getName().toLowerCase();");
486: lWriter
487: .println(" if (!lName.startsWith(pOperationFileNameBase.toLowerCase() + \".\"))");
488: lWriter.println(" return false;");
489: lWriter
490: .println(" if ((!lName.endsWith(\".xml\")) && (!lName.endsWith(\".xsl\")))");
491: lWriter.println(" return false;");
492: lWriter.println(" return true;");
493: lWriter.println(" }");
494: lWriter.println(" });");
495: lWriter.println(" }");
496: lWriter
497: .println(" if (lDataFiles == null || lDataFiles.length == 0)");
498: lWriter.println(" {");
499: lWriter
500: .println(" sLogger.debug(\"Unable to find any xml or xsl files with name starting with '\" + pOperationFileNameBase + \"' prefix.\");");
501: lWriter.println(" return null;");
502: lWriter.println(" }");
503: // Sort file names in natural alphabetical order and try to find the next file
504: // to the one we have used before
505: lWriter
506: .println(" Arrays.sort(lDataFiles, sFileComparator);");
507: lWriter.println(" int lNextFileIndex = 0;");
508: lWriter.println(" if (pPreviousDataFile != null)");
509: lWriter.println(" {");
510: lWriter
511: .println(" for (; lNextFileIndex < lDataFiles.length; lNextFileIndex++)");
512: lWriter.println(" {");
513: lWriter
514: .println(" if (lDataFiles[lNextFileIndex].equals(pPreviousDataFile))");
515: lWriter.println(" {");
516: lWriter.println(" lNextFileIndex++;");
517: lWriter.println(" break;");
518: lWriter.println(" }");
519: lWriter.println(" }");
520: lWriter.println(" }");
521: lWriter
522: .println(" return lNextFileIndex < lDataFiles.length ? lDataFiles[lNextFileIndex] : lDataFiles[0];");
523: lWriter.println(" }");
524: lWriter.println("}");
525: } catch (IOException e) {
526: throw new BSException("Unable to write file "
527: + lServiceImplementationFileName, e);
528: } finally {
529: if (lWriter != null) {
530: lWriter.flush();
531: lWriter.close();
532: }
533: if (lFileWriter != null) {
534: try {
535: lFileWriter.flush();
536: } catch (IOException e) {
537: // Ignore
538: }
539: try {
540: lFileWriter.close();
541: } catch (IOException e) {
542: // Ignore
543: }
544: }
545: }
546: }
547:
548: // Generates source for the main implementation class
549: private static void generateFactoryImplementationSource(
550: String pImplementationPackage, String pImplementationDir,
551: Service pService) throws BOException, BSException {
552: FileWriter lFileWriter = null;
553: PrintWriter lWriter = null;
554: // Generate service interface
555: try {
556: // Retrieve stylesheet for the servicemodule
557: STServiceStylesheet lServiceStylesheet = CodeGenerationStylesheetAccessor
558: .getServiceStylesheet(pService);
559: String lFileName = pImplementationDir
560: + File.separator
561: + lServiceStylesheet
562: .getImplementationFactoryClassName()
563: + ".java";
564: lWriter = new PrintWriter(lFileWriter = new FileWriter(
565: lFileName));
566: CopyrightUtils.writeCopyrightToGeneratedJavaSource(lWriter);
567: lWriter.println("package " + pImplementationPackage + ";");
568: lWriter.println();
569: lWriter.println("public class "
570: + lServiceStylesheet
571: .getImplementationFactoryClassName()
572: + " implements javax.naming.spi.ObjectFactory");
573: lWriter.println("{");
574: lWriter
575: .println(" private static Object sImplementationCreationSemaphore = new Object();");
576: lWriter.println(" private static "
577: + lServiceStylesheet.getImplementationClassName()
578: + " sImplementation = null;");
579:
580: lWriter
581: .println(" /* Returns the instance of the service */");
582: lWriter
583: .println(" public Object getObjectInstance(Object obj, javax.naming.Name name, javax.naming.Context nameCtx, java.util.Hashtable pEnvironment) throws Exception");
584: lWriter.println(" {");
585: lWriter
586: .println(" // Thread safe singleton implementaton - can create once and use all the time");
587: lWriter.println(" if (sImplementation == null)");
588: lWriter.println(" {");
589: lWriter
590: .println(" synchronized(sImplementationCreationSemaphore)");
591: lWriter.println(" {");
592: lWriter
593: .println(" if (sImplementation == null)");
594: lWriter
595: .println(" sImplementation = new "
596: + lServiceStylesheet
597: .getImplementationClassName()
598: + "(pEnvironment);");
599: lWriter.println(" }");
600: lWriter.println(" }");
601: lWriter.println(" return sImplementation;");
602: lWriter.println(" }");
603: lWriter.println("}");
604: } catch (IOException e) {
605: throw new BSException(
606: "Unable to generate implementation for the service. ServiceRef "
607: + pService.getRef(), e);
608: } finally {
609: if (lWriter != null) {
610: lWriter.flush();
611: lWriter.close();
612: }
613: if (lFileWriter != null) {
614: try {
615: lFileWriter.flush();
616: } catch (IOException e) {
617: // Ignore
618: }
619: try {
620: lFileWriter.close();
621: } catch (IOException e) {
622: // Ignore
623: }
624: }
625: }
626: }
627:
628: /* Returns the name of the package where adapter code is generated to */
629: public String getPackageNameForService(String pServiceRef)
630: throws BSException {
631: // This is a standard adapter - so stylesheet dictates the package name
632: STServiceStylesheet lServiceStylesheet = CodeGenerationStylesheetAccessor
633: .getServiceStylesheet(pServiceRef);
634: return lServiceStylesheet
635: .getGeneratedImplementationBasePackageName()
636: + ".xmldevsimulator";
637: }
638: }
|