001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2007, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.wsrp.producer.config.impl;
023:
024: import org.jboss.mx.util.MBeanServerLocator;
025: import org.jboss.mx.util.ObjectNameFactory;
026: import org.jboss.portal.common.net.URLTools;
027: import org.jboss.portal.jems.as.system.AbstractJBossService;
028: import org.jboss.portal.wsrp.producer.config.ProducerConfiguration;
029: import org.jboss.portal.wsrp.producer.config.ProducerConfigurationFactory;
030: import org.jboss.portal.wsrp.producer.config.ProducerConfigurationProvider;
031: import org.jboss.portal.wsrp.producer.config.ProducerConfigurationService;
032: import org.jboss.xb.binding.JBossXBException;
033: import org.jboss.xb.binding.ObjectModelFactory;
034: import org.jboss.xb.binding.ObjectModelProvider;
035: import org.jboss.xb.binding.Unmarshaller;
036: import org.jboss.xb.binding.UnmarshallerFactory;
037: import org.jboss.xb.binding.XercesXsMarshaller;
038: import org.jboss.xb.binding.sunday.unmarshalling.DefaultSchemaResolver;
039:
040: import javax.management.MBeanServer;
041: import javax.management.ObjectName;
042: import java.io.BufferedWriter;
043: import java.io.File;
044: import java.io.FileWriter;
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.InputStreamReader;
048: import java.io.Reader;
049: import java.io.StringWriter;
050: import java.io.Writer;
051: import java.net.URL;
052: import java.util.Enumeration;
053:
054: /**
055: * @author <a href="mailto:chris.laprun@jboss.com">Chris Laprun</a>
056: * @version $Revision: 9461 $
057: * @since 2.6
058: */
059: public class ProducerConfigurationServiceImpl extends
060: AbstractJBossService implements ProducerConfigurationService {
061: private String configLocation;
062: private ProducerConfiguration configuration;
063:
064: private File config;
065:
066: private static DefaultSchemaResolver RESOLVER;
067:
068: static {
069: RESOLVER = new DefaultSchemaResolver();
070: RESOLVER.setCacheResolvedSchemas(true);
071: RESOLVER.addSchemaLocation(
072: "http://www.w3.org/XML/1998/namespace", "xsd/xml.xsd");
073: RESOLVER.addSchemaLocation(
074: "urn:jboss:portal:wsrp:producer:v2_6",
075: "xsd/jboss-wsrp-producer_2_6.xsd");
076: }
077:
078: public String getConfigLocation() {
079: return configLocation;
080: }
081:
082: public void setConfigLocation(String configLocation) {
083: this .configLocation = configLocation;
084: }
085:
086: public ProducerConfiguration getConfiguration() {
087: return configuration;
088: }
089:
090: protected void startService() throws Exception {
091: MBeanServer server = MBeanServerLocator.locateJBoss();
092: ObjectName oname = ObjectNameFactory
093: .create("jboss.system:type=ServerConfig");
094: File dataDir;
095: try {
096: dataDir = (File) server
097: .getAttribute(oname, "ServerDataDir");
098: } catch (Exception e) {
099: throw new RuntimeException(
100: "Couldn't locate server data dir!", e);
101: }
102: config = new File(dataDir, "portal/wsrp-producer-config.xml");
103:
104: reloadConfiguration();
105: }
106:
107: public void reloadConfiguration() throws Exception {
108: URL configURL = getConfigLocationURL();
109:
110: try {
111: loadConfigurationAt(configURL);
112: } catch (Exception e) {
113: if (config.exists()) {
114: log
115: .debug("Configuration saved at "
116: + config.getCanonicalPath()
117: + " is not loading properly. Falling back to default configuration.");
118: config.delete(); // delete improper config so that we retrieve the default configuration
119: loadConfigurationAt(getConfigLocationURL());
120: }
121: }
122: }
123:
124: private void loadConfigurationAt(URL configURL)
125: throws JBossXBException, IOException {
126: log.debug("About to parse producer configuration " + configURL);
127: Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
128: .newUnmarshaller();
129: ObjectModelFactory factory = new ProducerConfigurationFactory();
130: configuration = (ProducerConfiguration) unmarshaller.unmarshal(
131: configURL.openStream(), factory, null);
132: }
133:
134: private URL getConfigLocationURL() throws Exception {
135: if (!config.exists()) {
136: // Setup URLs
137: if (configLocation == null) {
138: throw new Exception("The config location is null");
139: }
140:
141: // make sure we pick the furthest down the path in case there's several similarly named resources in the path
142: Enumeration resources = Thread.currentThread()
143: .getContextClassLoader().getResources(
144: configLocation);
145: URL configURL = null;
146: while (resources.hasMoreElements()) {
147: configURL = (URL) resources.nextElement();
148: }
149:
150: if (configURL == null) {
151: throw new Exception("The config " + configLocation
152: + " does not exist");
153: }
154: if (!URLTools.exists(configURL)) {
155: throw new Exception("The config " + configURL
156: + " does not exist");
157: }
158: return configURL;
159: } else {
160: return config.toURL();
161: }
162: }
163:
164: public void saveConfiguration() throws Exception {
165: // get the output writer to write the XML content
166: StringWriter xmlOutput = new StringWriter();
167:
168: // get the XML Schema source
169: InputStream is = Thread.currentThread().getContextClassLoader()
170: .getResourceAsStream("xsd/jboss-wsrp-producer_2_6.xsd");
171:
172: Reader xsReader = new InputStreamReader(is);
173:
174: // create an instance of XML Schema marshaller
175: XercesXsMarshaller marshaller = new XercesXsMarshaller();
176:
177: marshaller.setSchemaResolver(RESOLVER);
178:
179: // we need to specify what elements are top most (roots) providing namespace URI, prefix and local name
180: marshaller.addRootElement(
181: "urn:jboss:portal:wsrp:producer:v2_6", "",
182: "producer-configuration");
183:
184: // declare default namespace
185: marshaller.declareNamespace("wpc",
186: "urn:jboss:portal:wsrp:producer:v2_6");
187:
188: // add schema location by declaring xsi namespace and adding xsi:schemaLocation attribute
189: marshaller.declareNamespace("xsi",
190: "http://www.w3.org/2001/XMLSchema-instance");
191: marshaller
192: .addAttribute(
193: "xsi",
194: "schemaLocation",
195: "string",
196: "urn:jboss:portal:wsrp:producer:v2_6 http://www.jboss.org/portal/xsd/jboss-wsrp-producer_2_6.xsd");
197:
198: // create an instance of Object Model Provider
199: ObjectModelProvider provider = new ProducerConfigurationProvider();
200:
201: marshaller.setProperty(
202: "org.jboss.xml.binding.marshalling.indent", "true");
203: marshaller
204: .marshal(xsReader, provider, configuration, xmlOutput);
205:
206: // close XML Schema reader
207: xsReader.close();
208:
209: config.createNewFile();
210: Writer configFile = new BufferedWriter(new FileWriter(config));
211: configFile.write(xmlOutput.toString());
212: configFile.flush();
213: configFile.close();
214: }
215: }
|