Source Code Cross Referenced for Generator.java in  » UML » MetaBoss » com » metaboss » sdlctools » frameworks » generation » internal » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » UML » MetaBoss » com.metaboss.sdlctools.frameworks.generation.internal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.frameworks.generation.internal;
016:
017:        import java.io.File;
018:        import java.io.FileOutputStream;
019:        import java.io.FileWriter;
020:        import java.io.IOException;
021:        import java.io.InputStream;
022:        import java.io.OutputStream;
023:        import java.util.Iterator;
024:        import java.util.Map;
025:
026:        import javax.jmi.model.MofClass;
027:        import javax.naming.Context;
028:        import javax.naming.InitialContext;
029:
030:        import org.apache.commons.logging.Log;
031:        import org.apache.commons.logging.LogFactory;
032:
033:        import com.metaboss.enterprise.bs.BSException;
034:        import com.metaboss.enterprise.bs.BSNamingAndDirectoryServiceInvocationException;
035:        import com.metaboss.enterprise.bs.BSServiceProviderException;
036:        import com.metaboss.enterprise.bs.BSUnexpectedProgramConditionException;
037:        import com.metaboss.sdlctools.frameworks.generation.StylesheetAccessor;
038:        import com.metaboss.sdlctools.models.metabossmodel.ModelElement;
039:        import com.metaboss.sdlctools.services.jdktools.BSJamonTemplateProcessor;
040:        import com.metaboss.sdlctools.services.jdktools.BSJavaTemplateProcessor;
041:        import com.metaboss.sdlctools.services.jdktools.BSVelocityTemplateProcessor;
042:        import com.metaboss.sdlctools.services.jdktools.MergeResult;
043:        import com.metaboss.util.DirectoryUtils;
044:        import com.metaboss.util.StreamUtils;
045:        import com.metaboss.util.StringUtils;
046:
047:        /** Generator of developer's service simulator utilising XML files */
048:        public class Generator {
049:            // Commons Logging instance.
050:            private static final Log sLogger = LogFactory
051:                    .getLog(Generator.class);
052:            private static boolean sClassInitialised = false;
053:            private static Object sClassInitialisationSemaphore = new Object();
054:            private static BSJamonTemplateProcessor sJamonTemplateProcessor = null;
055:            private static BSVelocityTemplateProcessor sVelocityTemplateProcessor = null;
056:            private static BSJavaTemplateProcessor sJavaTemplateProcessor = null;
057:            private static StylesheetAccessor sStylesheetAccesor = new StylesheetAccessor();
058:
059:            // Pseudo class initialiser. Main feature that we can throw exception from here (unable to do that from native java class initialiser)
060:            private static void initialiseClassIfNecessary() throws BSException {
061:                if (!sClassInitialised) {
062:                    synchronized (sClassInitialisationSemaphore) {
063:                        if (!sClassInitialised) {
064:                            try {
065:                                Context lContext = new InitialContext();
066:                                sJamonTemplateProcessor = (BSJamonTemplateProcessor) lContext
067:                                        .lookup(BSJamonTemplateProcessor.COMPONENT_URL);
068:                                sVelocityTemplateProcessor = (BSVelocityTemplateProcessor) lContext
069:                                        .lookup(BSVelocityTemplateProcessor.COMPONENT_URL);
070:                                sJavaTemplateProcessor = (BSJavaTemplateProcessor) lContext
071:                                        .lookup(BSJavaTemplateProcessor.COMPONENT_URL);
072:                            } catch (javax.naming.NamingException e) {
073:                                throw new BSNamingAndDirectoryServiceInvocationException(
074:                                        "Unable to initialise Generator", e);
075:                            }
076:                            sClassInitialised = true;
077:                        }
078:                    }
079:                }
080:            }
081:
082:            /** Performs generation steps for the given element and all contained elements only as specified in the given generation plan
083:             * @param pDestinationDirectory - root directory to generate files to
084:             * @param pModelElement - the root element of the tree to use in generation. Note that nothing could be generated if no match for any of the elements in the tree has been found in the plan
085:             * @param pGenerationPlan generation plan to use
086:             * @param pTemplateContext context to pass to every generation template
087:             * @return Number of generated files */
088:            public static long doGenerationForElementTree(
089:                    String pDestinationDirectory, ModelElement pModelElement,
090:                    GenerationPlan pGenerationPlan, Map pTemplateContext)
091:                    throws BSException {
092:                initialiseClassIfNecessary();
093:
094:                // Do this element first
095:                long lFilesCount = doGenerationForElement(
096:                        pDestinationDirectory, pModelElement, pGenerationPlan,
097:                        pTemplateContext);
098:                // Now do the contents		
099:                for (Iterator lCombinedContentsIterator = pModelElement
100:                        .getCombinedContents().iterator(); lCombinedContentsIterator
101:                        .hasNext();)
102:                    lFilesCount += doGenerationForElement(
103:                            pDestinationDirectory,
104:                            (ModelElement) lCombinedContentsIterator.next(),
105:                            pGenerationPlan, pTemplateContext);
106:                return lFilesCount; // Number of generated files
107:            }
108:
109:            /** Performs generation steps for the given element only as specified in the given generation plan
110:             * @param pDestinationDirectory - root directory to generate files to
111:             * @param pModelElement - the element to use. Note that nothing could be generated if no match for the element found in the plan
112:             * @param pGenerationPlan generation plan to use
113:             * @param pTemplateContext context to pass to every generation template
114:             * @return Number of generated files */
115:            public static long doGenerationForElement(
116:                    String pDestinationDirectory, ModelElement pModelElement,
117:                    GenerationPlan pGenerationPlan, Map pTemplateContext)
118:                    throws BSException {
119:                initialiseClassIfNecessary();
120:
121:                long lFilesCount = 0;
122:
123:                // First attempt to locate generation details for the supplied element.
124:                // Iterate through all until found first matching details set
125:                GenerationPlan.GenerationDetails lGenerationDetailsForModelElement = null;
126:                String lModelElementRef = pModelElement.getRef();
127:                {
128:                    GenerationPlan.GenerationDetails[] lAllGenerationDetails = pGenerationPlan
129:                            .getGenerationDetails();
130:                    for (int i = 0; i < lAllGenerationDetails.length; i++) {
131:                        GenerationPlan.GenerationDetails lGenerationDetails = lAllGenerationDetails[i];
132:                        if (lModelElementRef
133:                                .matches(lGenerationDetails.mReferenceMatchingExpression)) {
134:                            lGenerationDetailsForModelElement = lGenerationDetails;
135:                            break;
136:                        }
137:                    }
138:                }
139:                if (lGenerationDetailsForModelElement != null) {
140:                    sLogger.debug("Processing " + lModelElementRef
141:                            + " for a plan "
142:                            + pGenerationPlan.getClass().getName()
143:                            + " into the directory " + pDestinationDirectory);
144:
145:                    // First process templates to find out source and destination fiel names
146:                    MofClass lModelElementMetaObject = (MofClass) pModelElement
147:                            .refMetaObject();
148:                    String lModelElementTypeName = (String) lModelElementMetaObject
149:                            .refGetValue("name");
150:                    java.util.Map lContextMap = new java.util.HashMap();
151:                    lContextMap.putAll(pTemplateContext);
152:                    lContextMap.put(lModelElementTypeName, pModelElement);
153:                    lContextMap.put("StylesheetAccessor", sStylesheetAccesor);
154:                    for (int i = 0; i < lGenerationDetailsForModelElement.mDestinationFileExpressions.length; i++) {
155:                        String lResourceName = null;
156:                        {
157:                            MergeResult lResult = sVelocityTemplateProcessor
158:                                    .mergeTemplate(
159:                                            lGenerationDetailsForModelElement.mResourceFileExpressions[i],
160:                                            "", lContextMap);
161:                            if (!lResult.isSuccessful()) {
162:                                // We handle problems here by loging the problem and returning model URI
163:                                throw new BSUnexpectedProgramConditionException(
164:                                        "Error processing template. Template processor log :"
165:                                                + lResult.getMergeLogPrintout());
166:                            }
167:                            lResourceName = lResult.getMergedOutput();
168:                        }
169:                        String lDestinationFileName = null;
170:                        {
171:                            MergeResult lResult = sVelocityTemplateProcessor
172:                                    .mergeTemplate(
173:                                            lGenerationDetailsForModelElement.mDestinationFileExpressions[i],
174:                                            "", lContextMap);
175:                            if (!lResult.isSuccessful()) {
176:                                // We handle problems here by loging the problem and returning model URI
177:                                throw new BSUnexpectedProgramConditionException(
178:                                        "Error processing template. Template processor log :"
179:                                                + lResult.getMergeLogPrintout());
180:                            }
181:                            lDestinationFileName = lResult.getMergedOutput()
182:                                    .trim();
183:                        }
184:                        // Process jamon template
185:                        try {
186:                            InputStream lInputStream = null;
187:                            try {
188:                                if ((lInputStream = pGenerationPlan
189:                                        .getResourceAsStream(lResourceName)) == null)
190:                                    throw new BSUnexpectedProgramConditionException(
191:                                            "Unable to locate resource: "
192:                                                    + lResourceName);
193:                                String lTemplate = StringUtils
194:                                        .readTextStreamFully(lInputStream);
195:                                MergeResult lResult = sJamonTemplateProcessor
196:                                        .mergeTemplate(lTemplate, StringUtils
197:                                                .replace(lResourceName, '.',
198:                                                        "_"), lContextMap);
199:                                if (!lResult.isSuccessful()) {
200:                                    // We handle problems here by loging the problem and returning model URI
201:                                    throw new BSUnexpectedProgramConditionException(
202:                                            "Error processing template. Template processor log :"
203:                                                    + lResult
204:                                                            .getMergeLogPrintout());
205:                                }
206:                                FileWriter lFileWriter = null;
207:                                try {
208:                                    File lDestinationFile = new File(
209:                                            pDestinationDirectory
210:                                                    + lDestinationFileName)
211:                                            .getAbsoluteFile();
212:                                    DirectoryUtils
213:                                            .ensureThereIsDirectory(lDestinationFile
214:                                                    .getParent());
215:                                    lFileWriter = new FileWriter(
216:                                            lDestinationFile);
217:                                    lFileWriter
218:                                            .write(lResult.getMergedOutput());
219:                                    lFileWriter.flush();
220:                                    lFilesCount++;
221:                                } finally {
222:                                    if (lFileWriter != null)
223:                                        lFileWriter.close();
224:                                }
225:                            } finally {
226:                                if (lInputStream != null)
227:                                    lInputStream.close();
228:                            }
229:                        } catch (IOException e) {
230:                            throw new BSServiceProviderException(
231:                                    "Unable to process Jamon template.", e);
232:                        }
233:                    }
234:                } else {
235:                    sLogger.debug("Ignoring " + lModelElementRef
236:                            + " for a plan "
237:                            + pGenerationPlan.getClass().getName()
238:                            + " (No processing defined for the element)");
239:                }
240:                return lFilesCount; // Number of generated files
241:            }
242:
243:            /** Performs copying steps as specified in supplied generation plan
244:             * @param pDestinationDirectory - root directory to copy files to
245:             * @param pGenerationPlan generation plan to use 
246:             * @return Number of copied files */
247:            public static long doCopying(String pDestinationDirectory,
248:                    GenerationPlan pGenerationPlan) throws BSException {
249:                initialiseClassIfNecessary();
250:
251:                long lFilesCount = 0;
252:
253:                GenerationPlan.CopyingDetails[] lCopyingDetails = pGenerationPlan
254:                        .getCopyingDetails();
255:                // First process templates to find out source and destination fiel names
256:                for (int i = 0; i < lCopyingDetails.length; i++) {
257:                    GenerationPlan.CopyingDetails lCopyingDetail = lCopyingDetails[i];
258:                    // Read and write file in 5k chunks
259:                    try {
260:                        InputStream lInputStream = null;
261:                        try {
262:                            if ((lInputStream = pGenerationPlan
263:                                    .getResourceAsStream(lCopyingDetail.mResourceFilePath)) == null)
264:                                throw new BSUnexpectedProgramConditionException(
265:                                        "Unable to locate resource: "
266:                                                + lCopyingDetail.mResourceFilePath);
267:                            File lDestinationFile = new File(
268:                                    pDestinationDirectory
269:                                            + lCopyingDetail.mDestinationFilePath)
270:                                    .getAbsoluteFile();
271:                            DirectoryUtils
272:                                    .ensureThereIsDirectory(lDestinationFile
273:                                            .getParent());
274:                            OutputStream lOutputStream = null;
275:                            try {
276:                                lOutputStream = new FileOutputStream(
277:                                        lDestinationFile, false);
278:                                StreamUtils.copyData(lInputStream,
279:                                        lOutputStream);
280:                                lOutputStream.flush();
281:                                lFilesCount++;
282:                            } finally {
283:                                if (lOutputStream != null)
284:                                    lOutputStream.close();
285:                            }
286:                        } finally {
287:                            if (lInputStream != null)
288:                                lInputStream.close();
289:                        }
290:                    } catch (IOException e) {
291:                        throw new BSServiceProviderException(
292:                                "Unable to copy resource files.", e);
293:                    }
294:                }
295:                return lFilesCount; // Number of copied files
296:            }
297:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.