KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > appservers > GenerateContainer


1 /*
2  * $Id: GenerateContainer.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.appservers;
25
26 import java.util.Map JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.io.*;
32
33 import org.ofbiz.base.container.Container;
34 import org.ofbiz.base.container.ContainerException;
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.template.FreeMarkerWorker;
37 import org.ofbiz.base.start.Classpath;
38 import org.ofbiz.base.component.ComponentConfig;
39
40 /**
41  * GenerateContainer - Generates Configuration Files For Application Servers
42  * ** This container requires StartInfoLoader to be loaded at startup.
43  * ** This container requires the ComponentContainer to be loaded first.
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 3.1
48  */

49 public class GenerateContainer implements Container {
50
51     public static final String JavaDoc module = GenerateContainer.class.getName();
52     public static final String JavaDoc source = "/framework/appservers/templates/";
53     public static final String JavaDoc target = "/setup/";
54
55     protected String JavaDoc configFile = null;
56     protected String JavaDoc ofbizHome = null;
57     protected String JavaDoc args[] = null;
58
59
60     /**
61      * @see org.ofbiz.base.container.Container#init(java.lang.String[], java.lang.String)
62      */

63     public void init(String JavaDoc[] args, String JavaDoc configFile) {
64         this.ofbizHome = System.getProperty("ofbiz.home");
65         this.configFile = configFile;
66         this.args = args;
67     }
68
69     /**
70      * @see org.ofbiz.base.container.Container#start()
71      */

72     public boolean start() throws ContainerException {
73         this.generateFiles();
74         System.exit(1);
75         return true;
76     }
77
78     /**
79      * Stop the container
80      *
81      * @throws org.ofbiz.base.container.ContainerException
82      *
83      */

84     public void stop() throws ContainerException {
85     }
86
87     private void generateFiles() throws ContainerException {
88         File files[] = getTemplates();
89         Map JavaDoc dataMap = buildDataMap();
90
91         //Debug.log("Using Data : " + dataMap, module);
92
for (int i = 0; i < files.length; i++) {
93             if (!files[i].isDirectory() && !files[i].isHidden()) {
94                 parseTemplate(files[i], dataMap);
95             }
96         }
97     }
98
99     private File[] getTemplates() throws ContainerException {
100         if (args == null) {
101             throw new ContainerException("Invalid application server type argument passed");
102         }
103
104         String JavaDoc templateLocation = args[0];
105         if (templateLocation == null) {
106             throw new ContainerException("Unable to locate Application Server template directory");
107         }
108
109         File parentDir = new File(ofbizHome + source + templateLocation);
110         if (!parentDir.exists() || !parentDir.isDirectory()) {
111             throw new ContainerException("Template location - " + templateLocation + " does not exist!");
112         }
113
114         return parentDir.listFiles();
115     }
116
117     private Map JavaDoc buildDataMap() {
118         Map JavaDoc dataMap = new HashMap JavaDoc();
119         List JavaDoc c[] = getClasspath();
120         dataMap.put("classpathJars", c[0]);
121         dataMap.put("classpathDirs", c[1]);
122         dataMap.put("env", System.getProperties());
123         dataMap.put("webApps", ComponentConfig.getAllWebappResourceInfos());
124         return dataMap;
125     }
126
127     private List JavaDoc[] getClasspath() {
128         Classpath classPath = new Classpath(System.getProperty("java.class.path"));
129         List JavaDoc elements = classPath.getElements();
130         List JavaDoc jar = new ArrayList JavaDoc();
131         List JavaDoc dir = new ArrayList JavaDoc();
132
133         Iterator JavaDoc i = elements.iterator();
134         while (i.hasNext()) {
135             File f = (File) i.next();
136             if (f.exists()) {
137                 if (f.isDirectory()) {
138                     dir.add(f.getAbsolutePath());
139                 } else {
140                     jar.add(f.getAbsolutePath());
141                 }
142             }
143         }
144
145         List JavaDoc[] lists = { jar, dir };
146         return lists;
147     }
148
149     private void parseTemplate(File templateFile, Map JavaDoc dataMap) throws ContainerException {
150         Debug.log("Parsing template : " + templateFile.getAbsolutePath(), module);
151         Reader reader = null;
152         try {
153             reader = new InputStreamReader(new FileInputStream(templateFile));
154         } catch (FileNotFoundException e) {
155             throw new ContainerException(e);
156         }
157
158         // create the target file/directory
159
String JavaDoc targetDirectoryName = args.length > 1 ? args[1] : null;
160         if (targetDirectoryName == null) {
161             targetDirectoryName = target;
162         }
163         String JavaDoc targetDirectory = ofbizHome + targetDirectoryName + args[0];
164         File targetDir = new File(targetDirectory);
165         if (!targetDir.exists()) {
166             boolean created = targetDir.mkdirs();
167             if (!created) {
168                 throw new ContainerException("Unable to create target directory - " + targetDirectory);
169             }
170         }
171
172         if (!targetDirectory.endsWith("/")) {
173             targetDirectory = targetDirectory + "/";
174         }
175
176         // write the template to the target directory
177
Writer writer = null;
178         try {
179             writer = new FileWriter(targetDirectory + templateFile.getName());
180         } catch (IOException e) {
181             throw new ContainerException(e);
182         }
183         try {
184             FreeMarkerWorker.renderTemplate(templateFile.getAbsolutePath(), reader, dataMap, writer);
185         } catch (Exception JavaDoc e) {
186             throw new ContainerException(e);
187         }
188
189         try {
190             writer.flush();
191             writer.close();
192         } catch (IOException e) {
193             throw new ContainerException(e);
194         }
195     }
196 }
197
Popular Tags