001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.support.glassfish;
022:
023: import com.sun.appserv.addons.AddonException;
024: import com.sun.appserv.addons.AddonVersion;
025: import com.sun.appserv.addons.ConfigurationContext.ConfigurationType;
026: import com.sun.appserv.addons.ConfigurationContext;
027: import com.sun.appserv.addons.Configurator;
028: import com.sun.appserv.addons.InstallationContext;
029: import com.sun.jbi.management.internal.support.JarFactory;
030: import com.sun.org.apache.xpath.internal.XPathAPI;
031:
032: import java.io.File;
033: import java.io.FileInputStream;
034: import java.io.FileOutputStream;
035:
036: import java.util.Properties;
037:
038: import javax.xml.parsers.DocumentBuilder;
039: import javax.xml.parsers.DocumentBuilderFactory;
040: import javax.xml.transform.OutputKeys;
041: import javax.xml.transform.Result;
042: import javax.xml.transform.Source;
043: import javax.xml.transform.Transformer;
044: import javax.xml.transform.TransformerFactory;
045: import javax.xml.transform.dom.DOMSource;
046: import javax.xml.transform.stream.StreamResult;
047:
048: import org.w3c.dom.Document;
049: import org.w3c.dom.Node;
050:
051: /**
052: * <a href="LiferayAddonConfigurator.java.html"><b><i>View Source</i></b></a>
053: *
054: * @author Raju Uppalapati
055: * @author Brian Wing Shun Chan
056: *
057: */
058: public class LiferayAddonConfigurator implements Configurator {
059:
060: public void configure(ConfigurationContext cc)
061: throws AddonException {
062: try {
063: doConfigure(cc);
064: } catch (Exception e) {
065: e.printStackTrace();
066:
067: throw new AddonException(e);
068: }
069: }
070:
071: public void disable(ConfigurationContext cc) throws AddonException {
072: }
073:
074: public void enable(ConfigurationContext cc) throws AddonException {
075: }
076:
077: public void unconfigure(ConfigurationContext cc)
078: throws AddonException {
079: try {
080: doUnconfigure(cc);
081: } catch (Exception e) {
082: e.printStackTrace();
083:
084: throw new AddonException(e);
085: }
086: }
087:
088: public void upgrade(ConfigurationContext cc, AddonVersion av)
089: throws AddonException {
090: }
091:
092: protected void doConfigure(ConfigurationContext cc)
093: throws Exception {
094: InstallationContext ic = cc.getInstallationContext();
095:
096: String rootDir = ic.getInstallationDirectory()
097: .getAbsolutePath();
098: String domainDir = cc.getDomainDirectory().getAbsolutePath();
099:
100: File tmpDir = LiferayAddonUtil.getTmpDir();
101:
102: JarFactory jarFactory = new JarFactory(tmpDir.getAbsolutePath());
103:
104: File liferayConfiguratorJar = new File(rootDir
105: + "/lib/addons/liferay_configurator.jar");
106:
107: jarFactory.unJar(liferayConfiguratorJar);
108:
109: LiferayAddonUtil.copyFile(tmpDir + "/lportal.properties",
110: domainDir + "/config/lportal.properties");
111:
112: LiferayAddonUtil.copyFile(tmpDir + "/lportal.script", domainDir
113: + "/config/lportal.script");
114:
115: StringBuffer jarFiles = new StringBuffer();
116: StringBuffer warFiles = new StringBuffer();
117:
118: File[] files = tmpDir.listFiles();
119:
120: for (int i = 0; i < files.length; i++) {
121: File file = (File) files[i];
122:
123: String name = file.getName().toLowerCase();
124:
125: if (name.endsWith(".jar")) {
126: LiferayAddonUtil.copyFile(file, new File(domainDir
127: + "/lib/" + file.getName()));
128:
129: jarFiles.append(file.getName());
130: jarFiles.append(":");
131: } else if (name.endsWith(".war")) {
132: LiferayAddonUtil.copyFile(file, new File(domainDir
133: + "/autodeploy/" + file.getName()));
134:
135: warFiles.append(file.getName());
136: warFiles.append(":");
137: }
138: }
139:
140: Properties props = new Properties();
141:
142: props.setProperty("jar.files", jarFiles.toString());
143: props.setProperty("war.files", warFiles.toString());
144:
145: File propsFile = new File(domainDir
146: + "/lib/liferay_addon.properties");
147:
148: FileOutputStream fos = new FileOutputStream(propsFile);
149:
150: props.store(fos, "");
151:
152: fos.close();
153:
154: ConfigurationType configurationType = cc.getConfigurationType();
155:
156: if (configurationType == ConfigurationType.DAS) {
157: configureDomainXml(rootDir + "/lib/dtds", new File(
158: domainDir + "/config/domain.xml"), new File(tmpDir
159: + "/update-domain.xml"));
160: }
161: }
162:
163: protected void doUnconfigure(ConfigurationContext cc)
164: throws Exception {
165: String domainDir = cc.getDomainDirectory().getAbsolutePath();
166:
167: Properties props = new Properties();
168:
169: File propsFile = new File(domainDir
170: + "/lib/liferay_addon.properties");
171:
172: FileInputStream fis = new FileInputStream(propsFile);
173:
174: props.load(fis);
175:
176: fis.close();
177:
178: String[] jarFiles = props.getProperty("jar.files", "").split(
179: ":");
180:
181: for (int i = 0; i < jarFiles.length; i++) {
182: File file = new File(domainDir + "/lib/" + jarFiles[i]);
183:
184: if (file.exists() && file.isFile()) {
185: file.delete();
186: }
187: }
188:
189: String[] warFiles = props.getProperty("war.files", "").split(
190: ":");
191:
192: for (int i = 0; i < warFiles.length; i++) {
193: File file = new File(domainDir + "/autodeploy/"
194: + warFiles[i]);
195:
196: if (file.exists() && file.isFile()) {
197: file.delete();
198: }
199: }
200:
201: propsFile.delete();
202:
203: ConfigurationType ct = cc.getConfigurationType();
204:
205: if (ct == ConfigurationType.DAS) {
206: }
207: }
208:
209: protected void configureDomainXml(String dtdDir,
210: File curDomainXmlFile, File newDomainXmlFile)
211: throws Exception {
212:
213: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
214: .newInstance();
215:
216: documentBuilderFactory.setValidating(false);
217:
218: DocumentBuilder documentBuilder = documentBuilderFactory
219: .newDocumentBuilder();
220:
221: DTDResolver dtdResolver = new DTDResolver(dtdDir);
222:
223: documentBuilder.setEntityResolver(dtdResolver);
224: documentBuilder.setErrorHandler(dtdResolver);
225:
226: Document curDomainXmlDoc = documentBuilder
227: .parse(curDomainXmlFile);
228:
229: Node curResourcesNode = XPathAPI.selectSingleNode(
230: curDomainXmlDoc, "/domain/resources");
231:
232: Node curJdbcResourceNode = XPathAPI.selectSingleNode(
233: curDomainXmlDoc, "/domain/resources/jdbc-resource");
234:
235: Node curJdbcConnectionPoolNode = XPathAPI.selectSingleNode(
236: curDomainXmlDoc,
237: "/domain/resources/jdbc-connection-pool");
238:
239: Node curServerNode = XPathAPI.selectSingleNode(curDomainXmlDoc,
240: "/domain/servers/server[@name='server']");
241:
242: Node curResourceRefNode = XPathAPI.selectSingleNode(
243: curDomainXmlDoc,
244: "/domain/servers/*/resource-ref[last()]");
245:
246: Document newDomainXmlDoc = documentBuilder
247: .parse(newDomainXmlFile);
248:
249: Node newJdbcResourceNode = XPathAPI.selectSingleNode(
250: newDomainXmlDoc, "/domain/resources/jdbc-resource");
251:
252: Node newMailResourceNode = XPathAPI.selectSingleNode(
253: newDomainXmlDoc, "/domain/resources/mail-resource");
254:
255: Node newJdbcConnectionPoolNode = XPathAPI.selectSingleNode(
256: newDomainXmlDoc,
257: "/domain/resources/jdbc-connection-pool");
258:
259: Node newLiferayPoolResourceRefNode = XPathAPI
260: .selectSingleNode(newDomainXmlDoc,
261: "/domain/servers/*/resource-ref[@ref='jdbc/LiferayPool']");
262:
263: Node newMailSessionResourceRefNode = XPathAPI
264: .selectSingleNode(newDomainXmlDoc,
265: "/domain/servers/*/resource-ref[@ref='mail/MailSession']");
266:
267: curResourcesNode.insertBefore(curDomainXmlDoc.importNode(
268: newJdbcResourceNode, true), curJdbcResourceNode);
269:
270: curResourcesNode.insertBefore(curDomainXmlDoc.importNode(
271: newMailResourceNode, true), curJdbcConnectionPoolNode);
272:
273: curResourcesNode.insertBefore(curDomainXmlDoc.importNode(
274: newJdbcConnectionPoolNode, true),
275: curJdbcConnectionPoolNode);
276:
277: curServerNode.insertBefore(curDomainXmlDoc.importNode(
278: newLiferayPoolResourceRefNode, true),
279: curResourceRefNode);
280:
281: curServerNode.insertBefore(curDomainXmlDoc.importNode(
282: newMailSessionResourceRefNode, true),
283: curResourceRefNode);
284:
285: LiferayAddonUtil.copyFile(curDomainXmlFile, new File(
286: curDomainXmlFile + ".bak."
287: + LiferayAddonUtil.getTimestamp()));
288:
289: Document curDoc = curResourcesNode.getOwnerDocument();
290:
291: Source input = new DOMSource(curDoc);
292:
293: Result output = new StreamResult(new FileOutputStream(
294: curDomainXmlFile));
295:
296: TransformerFactory transformerFactory = TransformerFactory
297: .newInstance();
298:
299: Transformer transformer = transformerFactory.newTransformer();
300:
301: transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, curDoc
302: .getDoctype().getPublicId());
303: transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, curDoc
304: .getDoctype().getSystemId());
305: transformer.setOutputProperty(OutputKeys.INDENT, "yes");
306: transformer.setOutputProperty(OutputKeys.METHOD, "xml");
307: transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
308: "no");
309:
310: transformer.transform(input, output);
311: }
312:
313: }
|