Author: chris.laprun(a)jboss.com
Date: 2009-09-24 12:07:57 -0400 (Thu, 24 Sep 2009)
New Revision: 203
Added:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/AbstractProducerConfigurationService.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/FileSystemXMLProducerConfigurationService.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/ProducerConfigurationProvider.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/SimpleXMLProducerConfigurationService.java
Removed:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/ProducerConfigurationProvider.java
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/ProducerConfigurationServiceImpl.java
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/ProducerConfigurationService.java
Log:
- Re-factored ProducerConfigurationService implementations to provide better opportunities
for alternate implementations:
+ Introduced AbstractProducerConfigurationService and
SimpleXMLProducerConfigurationService classes.
+ Renamed ProducerConfigurationServiceImpl to
FileSystemXMLProducerConfigurationService.
+ Introduced xml package and moved ProducerConfigurationProvider to it.
Deleted:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/ProducerConfigurationProvider.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/ProducerConfigurationProvider.java 2009-09-24
14:52:08 UTC (rev 202)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/ProducerConfigurationProvider.java 2009-09-24
16:07:57 UTC (rev 203)
@@ -1,189 +0,0 @@
-/*
- * JBoss, a division of Red Hat
- * Copyright 2009, Red Hat Middleware, LLC, and individual
- * contributors as indicated by the @authors tag. See the
- * copyright.txt in the distribution for a full listing of
- * individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.gatein.wsrp.producer.config;
-
-import org.gatein.registration.RegistrationPolicy;
-import org.gatein.registration.policies.DefaultRegistrationPolicy;
-import org.gatein.wsrp.WSRPUtils;
-import org.gatein.wsrp.producer.config.impl.ProducerConfigurationImpl;
-import org.gatein.wsrp.producer.config.impl.ProducerRegistrationRequirementsImpl;
-import org.gatein.wsrp.registration.LocalizedString;
-import org.gatein.wsrp.registration.RegistrationPropertyDescription;
-import org.jboss.xb.binding.MarshallingContext;
-import org.jboss.xb.binding.ObjectModelProvider;
-
-/**
- * Used to marshall Producer configuration to XML via JBoss XB.
- *
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision: 10408 $
- * @since 2.6.3
- */
-public class ProducerConfigurationProvider implements ObjectModelProvider
-{
- public Object getRoot(Object o, MarshallingContext marshallingContext, String s,
String s1)
- {
- return o;
- }
-
- public Object getChildren(ProducerConfigurationImpl configuration, String
namespaceUri, String localName)
- {
- if ("registration-configuration".equals(localName))
- {
- ProducerRegistrationRequirements registrationRequirements =
configuration.getRegistrationRequirements();
- if (registrationRequirements != null &&
registrationRequirements.isRegistrationRequired())
- {
- return registrationRequirements;
- }
- }
- else if ("producer-configuration".equals(localName))
- {
- return configuration;
- }
-
- return null;
- }
-
- public Object getChildren(ProducerRegistrationRequirementsImpl regReq, String
namespaceUri, String localName)
- {
- if ("registration-property-description".equals(localName))
- {
- return regReq.getRegistrationProperties().values();
- }
-
- return null;
- }
-
- public Object getAttributeValue(ProducerConfigurationImpl configuration, String
namespaceUri, String localName)
- {
- if ("useStrictMode".equals(localName))
- {
- return configuration.isUsingStrictMode();
- }
-
- return null;
- }
-
- public Object getAttributeValue(ProducerRegistrationRequirementsImpl regReq, String
namespaceUri, String localName)
- {
- if ("fullServiceDescriptionRequiresRegistration".equals(localName))
- {
- return regReq.isRegistrationRequiredForFullDescription();
- }
-
- return null;
- }
-
- public Object getElementValue(ProducerRegistrationRequirementsImpl regReq, String
namespaceUri, String localName)
- {
- Object value = null;
- if ("registration-policy".equals(localName))
- {
- RegistrationPolicy policy = regReq.getPolicy();
- if (policy != null)
- {
- value = policy.getClass().getName();
- }
- }
- else if ("registration-property-validator".equals(localName))
- {
- RegistrationPolicy policy = regReq.getPolicy();
- if (policy instanceof DefaultRegistrationPolicy)
- {
- DefaultRegistrationPolicy defaultRegistrationPolicy =
(DefaultRegistrationPolicy)policy;
- value = defaultRegistrationPolicy.getValidator().getClass().getName();
- }
- }
- else
- {
- value = null;
- }
- return value;
- }
-
- public Object getElementValue(RegistrationPropertyDescription propertyDescription,
String namespaceUri, String localName)
- {
- Object value = null;
- if ("name".equals(localName))
- {
- value = propertyDescription.getName();
- }
- else if ("type".equals(localName))
- {
- value = propertyDescription.getType();
- }
- else if ("label".equals(localName))
- {
- value = getLocalizedStringOrNull(propertyDescription.getLabel());
- }
- else if ("hint".equals(localName))
- {
- value = getLocalizedStringOrNull(propertyDescription.getHint());
- }
- else if ("description".equals(localName))
- {
- value = getLocalizedStringOrNull(propertyDescription.getDescription());
- }
- return value;
- }
-
- private LocalizedString getLocalizedStringOrNull(LocalizedString string)
- {
- if (string != null)
- {
- String value = string.getValue();
- if (value == null || value.length() == 0)
- {
- return null;
- }
- else
- {
- return string;
- }
- }
- else
- {
- return null;
- }
- }
-
- public Object getAttributeValue(LocalizedString localizedString, String namespaceUri,
String localName)
- {
- Object value = null;
- if ("lang".equals(localName))
- {
- value = WSRPUtils.toString(localizedString.getLocale());
- }
- else if ("resourceName".equals(localName))
- {
- value = localizedString.getResourceName();
- }
- return value;
- }
-
- public Object getElementValue(LocalizedString localizedString, String namespaceUri,
String localName)
- {
- return localizedString.getValue();
- }
-}
Modified:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/ProducerConfigurationService.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/ProducerConfigurationService.java 2009-09-24
14:52:08 UTC (rev 202)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/ProducerConfigurationService.java 2009-09-24
16:07:57 UTC (rev 203)
@@ -23,6 +23,8 @@
package org.gatein.wsrp.producer.config;
+import java.io.InputStream;
+
/**
* @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
* @version $Revision: 8784 $
@@ -47,4 +49,12 @@
* @since 2.6.3
*/
void saveConfiguration() throws Exception;
+
+ /**
+ * Loads a producer configuration from the given InputStream.
+ *
+ * @param inputStream the InputStream to load the configuration from
+ * @throws Exception
+ */
+ void loadConfigurationFrom(InputStream inputStream) throws Exception;
}
Added:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/AbstractProducerConfigurationService.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/AbstractProducerConfigurationService.java
(rev 0)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/AbstractProducerConfigurationService.java 2009-09-24
16:07:57 UTC (rev 203)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2009, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.gatein.wsrp.producer.config.impl;
+
+import org.gatein.common.util.ParameterValidation;
+import org.gatein.registration.RegistrationPolicyChangeListener;
+import org.gatein.registration.RegistrationPropertyChangeListener;
+import org.gatein.wsrp.producer.config.ProducerConfiguration;
+import org.gatein.wsrp.producer.config.ProducerConfigurationChangeListener;
+import org.gatein.wsrp.producer.config.ProducerConfigurationService;
+import org.gatein.wsrp.producer.config.ProducerRegistrationRequirements;
+
+import java.io.InputStream;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public abstract class AbstractProducerConfigurationService implements
ProducerConfigurationService
+{
+ protected ProducerConfiguration configuration;
+
+ public ProducerConfiguration getConfiguration()
+ {
+ return configuration;
+ }
+
+ public void loadConfigurationFrom(InputStream inputStream) throws Exception
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(inputStream,
"InputStream");
+
+ // save listeners if we already have a configuration
+ List<ProducerConfigurationChangeListener> listeners = null;
+ Set<RegistrationPolicyChangeListener> policyListeners = null;
+ Set<RegistrationPropertyChangeListener> propertyListeners = null;
+ ProducerRegistrationRequirements registrationRequirements;
+ if (configuration != null)
+ {
+ listeners = configuration.getChangeListeners();
+ registrationRequirements = configuration.getRegistrationRequirements();
+ if (registrationRequirements != null)
+ {
+ policyListeners = registrationRequirements.getPolicyChangeListeners();
+ propertyListeners = registrationRequirements.getPropertyChangeListeners();
+ }
+ }
+
+ // reload
+ configuration = parseConfigurationFrom(inputStream);
+
+ // restore listeners
+ if (listeners != null)
+ {
+ for (ProducerConfigurationChangeListener listener : listeners)
+ {
+ configuration.addChangeListener(listener);
+ }
+ }
+ registrationRequirements = configuration.getRegistrationRequirements();
+ if (registrationRequirements != null)
+ {
+ if (propertyListeners != null)
+ {
+ for (RegistrationPropertyChangeListener listener : propertyListeners)
+ {
+ registrationRequirements.addRegistrationPropertyChangeListener(listener);
+ }
+ }
+ if (policyListeners != null)
+ {
+ for (RegistrationPolicyChangeListener listener : policyListeners)
+ {
+ registrationRequirements.addRegistrationPolicyChangeListener(listener);
+ }
+ }
+ }
+ }
+
+ protected abstract ProducerConfiguration parseConfigurationFrom(InputStream
inputStream) throws Exception;
+
+}
Deleted:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/ProducerConfigurationServiceImpl.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/ProducerConfigurationServiceImpl.java 2009-09-24
14:52:08 UTC (rev 202)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/ProducerConfigurationServiceImpl.java 2009-09-24
16:07:57 UTC (rev 203)
@@ -1,291 +0,0 @@
-/*
- * JBoss, a division of Red Hat
- * Copyright 2009, Red Hat Middleware, LLC, and individual
- * contributors as indicated by the @authors tag. See the
- * copyright.txt in the distribution for a full listing of
- * individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.gatein.wsrp.producer.config.impl;
-
-import org.gatein.common.net.URLTools;
-import org.gatein.registration.RegistrationPolicyChangeListener;
-import org.gatein.registration.RegistrationPropertyChangeListener;
-import org.gatein.wsrp.producer.config.ProducerConfiguration;
-import org.gatein.wsrp.producer.config.ProducerConfigurationChangeListener;
-import org.gatein.wsrp.producer.config.ProducerConfigurationFactory;
-import org.gatein.wsrp.producer.config.ProducerConfigurationProvider;
-import org.gatein.wsrp.producer.config.ProducerConfigurationService;
-import org.gatein.wsrp.producer.config.ProducerRegistrationRequirements;
-import org.jboss.xb.binding.JBossXBException;
-import org.jboss.xb.binding.ObjectModelFactory;
-import org.jboss.xb.binding.ObjectModelProvider;
-import org.jboss.xb.binding.Unmarshaller;
-import org.jboss.xb.binding.UnmarshallerFactory;
-import org.jboss.xb.binding.XercesXsMarshaller;
-import org.jboss.xb.binding.sunday.unmarshalling.DefaultSchemaResolver;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Set;
-
-/**
- * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
- * @version $Revision: 12276 $
- * @since 2.6
- */
-public class ProducerConfigurationServiceImpl implements ProducerConfigurationService
-{
- private static final Logger log =
LoggerFactory.getLogger(ProducerConfigurationServiceImpl.class);
-
- private String configLocation;
- private ProducerConfiguration configuration;
-
- private File config;
-
- private static DefaultSchemaResolver RESOLVER;
-
- private static final String PRODUCER_NAMESPACE =
"urn:jboss:portal:wsrp:producer:v2_7";
- private static final String PRODUCER_XSD = "jboss-wsrp-producer_2_7.xsd";
-
- static
- {
- RESOLVER = new DefaultSchemaResolver();
- RESOLVER.setCacheResolvedSchemas(true);
-
RESOLVER.addSchemaLocation("http://www.w3.org/XML/1998/namespace",
"xsd/xml.xsd");
- RESOLVER.addSchemaLocation("urn:jboss:portal:wsrp:producer:v2_6",
"xsd/jboss-wsrp-producer_2_6.xsd");
- RESOLVER.addSchemaLocation(PRODUCER_NAMESPACE, "xsd/" + PRODUCER_XSD);
- }
-
- public String getConfigLocation()
- {
- return configLocation;
- }
-
- public void setConfigLocation(String configLocation)
- {
- this.configLocation = configLocation;
- }
-
-
- public ProducerConfiguration getConfiguration()
- {
- return configuration;
- }
-
- public void start() throws Exception
- {
- File dataDir;
- /*MBeanServer server = MBeanServerLocator.locateJBoss();
- ObjectName oname =
ObjectNameFactory.create("jboss.system:type=ServerConfig");
- try
- {
- dataDir = (File)server.getAttribute(oname, "ServerDataDir");
- }
- catch (Exception e)
- {
- throw new RuntimeException("Couldn't locate server data dir!",
e);
- }*/
-
- // todo: replace by proper location or JCR-based persistence
- dataDir = new File(System.getProperty("java.io.tmpdir"));
-
- // if "portal" directory doesn't exist already in data, create it
(JBPORTAL-2229)
- File portalDir = new File(dataDir, "portal");
- if (!portalDir.exists())
- {
- if (!portalDir.mkdir())
- {
- throw new RuntimeException("Couldn't create 'portal'
directory in " + dataDir.getAbsolutePath());
- }
- }
- else
- {
- if (!portalDir.isDirectory())
- {
- throw new RuntimeException("Was expecting a directory named
'portal' in " + dataDir.getAbsolutePath()
- + ", not a simple file! Cannot continue.");
- }
- }
-
- config = new File(portalDir, "wsrp-producer-config.xml");
-
- reloadConfiguration();
- }
-
- public void reloadConfiguration() throws Exception
- {
- URL configURL = getConfigLocationURL();
-
- try
- {
- loadConfigurationAt(configURL);
- }
- catch (Exception e)
- {
- if (config.exists())
- {
- log.debug("Configuration saved at " + config.getCanonicalPath()
- + " is not loading properly. Falling back to default
configuration.");
- config.delete(); // delete improper config so that we retrieve the default
configuration
- loadConfigurationAt(getConfigLocationURL());
- }
- }
- }
-
- private void loadConfigurationAt(URL configURL) throws JBossXBException, IOException
- {
- log.debug("About to parse producer configuration " + configURL);
- Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
- ObjectModelFactory factory = new ProducerConfigurationFactory();
-
- // save listeners if we already have a configuration
- List<ProducerConfigurationChangeListener> listeners = null;
- Set<RegistrationPolicyChangeListener> policyListeners = null;
- Set<RegistrationPropertyChangeListener> propertyListeners = null;
- ProducerRegistrationRequirements registrationRequirements;
- if (configuration != null)
- {
- listeners = configuration.getChangeListeners();
- registrationRequirements = configuration.getRegistrationRequirements();
- if (registrationRequirements != null)
- {
- policyListeners = registrationRequirements.getPolicyChangeListeners();
- propertyListeners = registrationRequirements.getPropertyChangeListeners();
- }
- }
-
- // reload
- configuration =
(ProducerConfiguration)unmarshaller.unmarshal(configURL.openStream(), factory, null);
-
- // restore listeners
- if (listeners != null)
- {
- for (ProducerConfigurationChangeListener listener : listeners)
- {
- configuration.addChangeListener(listener);
- }
- }
- registrationRequirements = configuration.getRegistrationRequirements();
- if (registrationRequirements != null)
- {
- if (propertyListeners != null)
- {
- for (RegistrationPropertyChangeListener listener : propertyListeners)
- {
- registrationRequirements.addRegistrationPropertyChangeListener(listener);
- }
- }
- if (policyListeners != null)
- {
- for (RegistrationPolicyChangeListener listener : policyListeners)
- {
- registrationRequirements.addRegistrationPolicyChangeListener(listener);
- }
- }
- }
- }
-
- private URL getConfigLocationURL() throws Exception
- {
- if (!config.exists())
- {
- // Setup URLs
- if (configLocation == null)
- {
- throw new Exception("The config location is null");
- }
-
- // make sure we pick the furthest down the path in case there's several
similarly named resources in the path
- Enumeration resources =
Thread.currentThread().getContextClassLoader().getResources(configLocation);
- URL configURL = null;
- while (resources.hasMoreElements())
- {
- configURL = (URL)resources.nextElement();
- }
-
- if (configURL == null)
- {
- throw new Exception("The config " + configLocation + " does
not exist");
- }
- if (!URLTools.exists(configURL))
- {
- throw new Exception("The config " + configURL + " does not
exist");
- }
- return configURL;
- }
- else
- {
- return config.toURI().toURL();
- }
- }
-
- public void saveConfiguration() throws Exception
- {
- // get the output writer to write the XML content
- StringWriter xmlOutput = new StringWriter();
-
- // get the XML Schema source
- InputStream is =
Thread.currentThread().getContextClassLoader().getResourceAsStream("xsd/" +
PRODUCER_XSD);
-
- Reader xsReader = new InputStreamReader(is);
-
- // create an instance of XML Schema marshaller
- XercesXsMarshaller marshaller = new XercesXsMarshaller();
-
- marshaller.setSchemaResolver(RESOLVER);
-
- // we need to specify what elements are top most (roots) providing namespace URI,
prefix and local name
- marshaller.addRootElement(PRODUCER_NAMESPACE, "",
"producer-configuration");
-
- // declare default namespace
- marshaller.declareNamespace("wpc", PRODUCER_NAMESPACE);
-
- // add schema location by declaring xsi namespace and adding xsi:schemaLocation
attribute
- marshaller.declareNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance");
- marshaller.addAttribute("xsi", "schemaLocation",
"string",
- PRODUCER_NAMESPACE + "
http://www.jboss.org/portal/xsd/" +
PRODUCER_XSD);
-
- // create an instance of Object Model Provider
- ObjectModelProvider provider = new ProducerConfigurationProvider();
-
- marshaller.setProperty("org.jboss.xml.binding.marshalling.indent",
"true");
- marshaller.marshal(xsReader, provider, configuration, xmlOutput);
-
- // close XML Schema reader
- xsReader.close();
-
- config.createNewFile();
- Writer configFile = new BufferedWriter(new FileWriter(config));
- configFile.write(xmlOutput.toString());
- configFile.flush();
- configFile.close();
- }
-}
Added:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/FileSystemXMLProducerConfigurationService.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/FileSystemXMLProducerConfigurationService.java
(rev 0)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/FileSystemXMLProducerConfigurationService.java 2009-09-24
16:07:57 UTC (rev 203)
@@ -0,0 +1,216 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2009, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.gatein.wsrp.producer.config.impl.xml;
+
+import org.gatein.common.net.URLTools;
+import org.jboss.xb.binding.ObjectModelProvider;
+import org.jboss.xb.binding.XercesXsMarshaller;
+import org.jboss.xb.binding.sunday.unmarshalling.DefaultSchemaResolver;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.net.URL;
+import java.util.Enumeration;
+
+/**
+ * A ProducerConfigurationService that can load an initial seed of an XML configuration
file and then store
+ * modifications made to it on the file system, in the
<code>${java.io.tmpdir}/portal/wsrp-producer-config.xml</code>
+ * file.
+ *
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 12276 $
+ * @since 2.6
+ */
+public class FileSystemXMLProducerConfigurationService extends
SimpleXMLProducerConfigurationService
+{
+ private static final Logger log =
LoggerFactory.getLogger(FileSystemXMLProducerConfigurationService.class);
+
+ private String configLocation;
+
+ private File config;
+
+ private static DefaultSchemaResolver RESOLVER;
+
+ private static final String PRODUCER_NAMESPACE =
"urn:jboss:portal:wsrp:producer:v2_7";
+ private static final String PRODUCER_XSD = "jboss-wsrp-producer_2_7.xsd";
+
+ static
+ {
+ RESOLVER = new DefaultSchemaResolver();
+ RESOLVER.setCacheResolvedSchemas(true);
+
RESOLVER.addSchemaLocation("http://www.w3.org/XML/1998/namespace",
"xsd/xml.xsd");
+ RESOLVER.addSchemaLocation("urn:jboss:portal:wsrp:producer:v2_6",
"xsd/jboss-wsrp-producer_2_6.xsd");
+ RESOLVER.addSchemaLocation(PRODUCER_NAMESPACE, "xsd/" + PRODUCER_XSD);
+ }
+
+ public String getConfigLocation()
+ {
+ return configLocation;
+ }
+
+ public void setConfigLocation(String configLocation)
+ {
+ this.configLocation = configLocation;
+ }
+
+
+ public void start() throws Exception
+ {
+ File dataDir = new File(System.getProperty("java.io.tmpdir"));
+
+ // if "portal" directory doesn't exist already in data, create it
(JBPORTAL-2229)
+ File portalDir = new File(dataDir, "portal");
+ if (!portalDir.exists())
+ {
+ if (!portalDir.mkdir())
+ {
+ throw new RuntimeException("Couldn't create 'portal'
directory in " + dataDir.getAbsolutePath());
+ }
+ }
+ else
+ {
+ if (!portalDir.isDirectory())
+ {
+ throw new RuntimeException("Was expecting a directory named
'portal' in " + dataDir.getAbsolutePath()
+ + ", not a simple file! Cannot continue.");
+ }
+ }
+
+ config = new File(portalDir, "wsrp-producer-config.xml");
+
+ reloadConfiguration();
+ }
+
+ public void reloadConfiguration() throws Exception
+ {
+ URL configURL = getConfigLocationURL();
+
+ try
+ {
+ loadConfigurationAt(configURL);
+ }
+ catch (Exception e)
+ {
+ if (config.exists())
+ {
+ log.debug("Configuration saved at " + config.getCanonicalPath()
+ + " is not loading properly. Falling back to default
configuration.");
+ config.delete(); // delete improper config so that we retrieve the default
configuration
+ loadConfigurationAt(getConfigLocationURL());
+ }
+ }
+ }
+
+ private void loadConfigurationAt(URL configURL) throws Exception
+ {
+ log.debug("About to parse producer configuration " + configURL);
+ InputStream inputStream = configURL.openStream();
+
+ loadConfigurationFrom(inputStream);
+ }
+
+ private URL getConfigLocationURL() throws Exception
+ {
+ if (!config.exists())
+ {
+ // Setup URLs
+ if (configLocation == null)
+ {
+ throw new Exception("The config location is null");
+ }
+
+ // make sure we pick the furthest down the path in case there's several
similarly named resources in the path
+ Enumeration resources =
Thread.currentThread().getContextClassLoader().getResources(configLocation);
+ URL configURL = null;
+ while (resources.hasMoreElements())
+ {
+ configURL = (URL)resources.nextElement();
+ }
+
+ if (configURL == null)
+ {
+ throw new Exception("The config " + configLocation + " does
not exist");
+ }
+ if (!URLTools.exists(configURL))
+ {
+ throw new Exception("The config " + configURL + " does not
exist");
+ }
+ return configURL;
+ }
+ else
+ {
+ return config.toURI().toURL();
+ }
+ }
+
+ public void saveConfiguration() throws Exception
+ {
+ // get the output writer to write the XML content
+ StringWriter xmlOutput = new StringWriter();
+
+ // get the XML Schema source
+ InputStream is =
Thread.currentThread().getContextClassLoader().getResourceAsStream("xsd/" +
PRODUCER_XSD);
+
+ Reader xsReader = new InputStreamReader(is);
+
+ // create an instance of XML Schema marshaller
+ XercesXsMarshaller marshaller = new XercesXsMarshaller();
+
+ marshaller.setSchemaResolver(RESOLVER);
+
+ // we need to specify what elements are top most (roots) providing namespace URI,
prefix and local name
+ marshaller.addRootElement(PRODUCER_NAMESPACE, "",
"producer-configuration");
+
+ // declare default namespace
+ marshaller.declareNamespace("wpc", PRODUCER_NAMESPACE);
+
+ // add schema location by declaring xsi namespace and adding xsi:schemaLocation
attribute
+ marshaller.declareNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance");
+ marshaller.addAttribute("xsi", "schemaLocation",
"string",
+ PRODUCER_NAMESPACE + "
http://www.jboss.org/portal/xsd/" +
PRODUCER_XSD);
+
+ // create an instance of Object Model Provider
+ ObjectModelProvider provider = new ProducerConfigurationProvider();
+
+ marshaller.setProperty("org.jboss.xml.binding.marshalling.indent",
"true");
+ marshaller.marshal(xsReader, provider, configuration, xmlOutput);
+
+ // close XML Schema reader
+ xsReader.close();
+
+ config.createNewFile();
+ Writer configFile = new BufferedWriter(new FileWriter(config));
+ configFile.write(xmlOutput.toString());
+ configFile.flush();
+ configFile.close();
+ }
+}
Added:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/ProducerConfigurationProvider.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/ProducerConfigurationProvider.java
(rev 0)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/ProducerConfigurationProvider.java 2009-09-24
16:07:57 UTC (rev 203)
@@ -0,0 +1,190 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2009, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.gatein.wsrp.producer.config.impl.xml;
+
+import org.gatein.registration.RegistrationPolicy;
+import org.gatein.registration.policies.DefaultRegistrationPolicy;
+import org.gatein.wsrp.WSRPUtils;
+import org.gatein.wsrp.producer.config.ProducerRegistrationRequirements;
+import org.gatein.wsrp.producer.config.impl.ProducerConfigurationImpl;
+import org.gatein.wsrp.producer.config.impl.ProducerRegistrationRequirementsImpl;
+import org.gatein.wsrp.registration.LocalizedString;
+import org.gatein.wsrp.registration.RegistrationPropertyDescription;
+import org.jboss.xb.binding.MarshallingContext;
+import org.jboss.xb.binding.ObjectModelProvider;
+
+/**
+ * Used to marshall Producer configuration to XML via JBoss XB.
+ *
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 10408 $
+ * @since 2.6.3
+ */
+public class ProducerConfigurationProvider implements ObjectModelProvider
+{
+ public Object getRoot(Object o, MarshallingContext marshallingContext, String s,
String s1)
+ {
+ return o;
+ }
+
+ public Object getChildren(ProducerConfigurationImpl configuration, String
namespaceUri, String localName)
+ {
+ if ("registration-configuration".equals(localName))
+ {
+ ProducerRegistrationRequirements registrationRequirements =
configuration.getRegistrationRequirements();
+ if (registrationRequirements != null &&
registrationRequirements.isRegistrationRequired())
+ {
+ return registrationRequirements;
+ }
+ }
+ else if ("producer-configuration".equals(localName))
+ {
+ return configuration;
+ }
+
+ return null;
+ }
+
+ public Object getChildren(ProducerRegistrationRequirementsImpl regReq, String
namespaceUri, String localName)
+ {
+ if ("registration-property-description".equals(localName))
+ {
+ return regReq.getRegistrationProperties().values();
+ }
+
+ return null;
+ }
+
+ public Object getAttributeValue(ProducerConfigurationImpl configuration, String
namespaceUri, String localName)
+ {
+ if ("useStrictMode".equals(localName))
+ {
+ return configuration.isUsingStrictMode();
+ }
+
+ return null;
+ }
+
+ public Object getAttributeValue(ProducerRegistrationRequirementsImpl regReq, String
namespaceUri, String localName)
+ {
+ if ("fullServiceDescriptionRequiresRegistration".equals(localName))
+ {
+ return regReq.isRegistrationRequiredForFullDescription();
+ }
+
+ return null;
+ }
+
+ public Object getElementValue(ProducerRegistrationRequirementsImpl regReq, String
namespaceUri, String localName)
+ {
+ Object value = null;
+ if ("registration-policy".equals(localName))
+ {
+ RegistrationPolicy policy = regReq.getPolicy();
+ if (policy != null)
+ {
+ value = policy.getClass().getName();
+ }
+ }
+ else if ("registration-property-validator".equals(localName))
+ {
+ RegistrationPolicy policy = regReq.getPolicy();
+ if (policy instanceof DefaultRegistrationPolicy)
+ {
+ DefaultRegistrationPolicy defaultRegistrationPolicy =
(DefaultRegistrationPolicy)policy;
+ value = defaultRegistrationPolicy.getValidator().getClass().getName();
+ }
+ }
+ else
+ {
+ value = null;
+ }
+ return value;
+ }
+
+ public Object getElementValue(RegistrationPropertyDescription propertyDescription,
String namespaceUri, String localName)
+ {
+ Object value = null;
+ if ("name".equals(localName))
+ {
+ value = propertyDescription.getName();
+ }
+ else if ("type".equals(localName))
+ {
+ value = propertyDescription.getType();
+ }
+ else if ("label".equals(localName))
+ {
+ value = getLocalizedStringOrNull(propertyDescription.getLabel());
+ }
+ else if ("hint".equals(localName))
+ {
+ value = getLocalizedStringOrNull(propertyDescription.getHint());
+ }
+ else if ("description".equals(localName))
+ {
+ value = getLocalizedStringOrNull(propertyDescription.getDescription());
+ }
+ return value;
+ }
+
+ private LocalizedString getLocalizedStringOrNull(LocalizedString string)
+ {
+ if (string != null)
+ {
+ String value = string.getValue();
+ if (value == null || value.length() == 0)
+ {
+ return null;
+ }
+ else
+ {
+ return string;
+ }
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ public Object getAttributeValue(LocalizedString localizedString, String namespaceUri,
String localName)
+ {
+ Object value = null;
+ if ("lang".equals(localName))
+ {
+ value = WSRPUtils.toString(localizedString.getLocale());
+ }
+ else if ("resourceName".equals(localName))
+ {
+ value = localizedString.getResourceName();
+ }
+ return value;
+ }
+
+ public Object getElementValue(LocalizedString localizedString, String namespaceUri,
String localName)
+ {
+ return localizedString.getValue();
+ }
+}
Added:
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/SimpleXMLProducerConfigurationService.java
===================================================================
---
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/SimpleXMLProducerConfigurationService.java
(rev 0)
+++
components/wsrp/trunk/producer/src/main/java/org/gatein/wsrp/producer/config/impl/xml/SimpleXMLProducerConfigurationService.java 2009-09-24
16:07:57 UTC (rev 203)
@@ -0,0 +1,63 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2009, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.gatein.wsrp.producer.config.impl.xml;
+
+import org.gatein.wsrp.producer.config.ProducerConfiguration;
+import org.gatein.wsrp.producer.config.ProducerConfigurationFactory;
+import org.gatein.wsrp.producer.config.impl.AbstractProducerConfigurationService;
+import org.jboss.xb.binding.ObjectModelFactory;
+import org.jboss.xb.binding.Unmarshaller;
+import org.jboss.xb.binding.UnmarshallerFactory;
+
+import java.io.InputStream;
+
+/**
+ * A simple configuration service that only supports manually edits to an external XML
configuration file. Reloading and
+ * saving modifications are therefore not supported.
+ *
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision: 12276 $
+ * @since 2.6
+ */
+public class SimpleXMLProducerConfigurationService extends
AbstractProducerConfigurationService
+{
+
+ @Override
+ protected ProducerConfiguration parseConfigurationFrom(InputStream inputStream) throws
Exception
+ {
+ Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
+ ObjectModelFactory factory = new ProducerConfigurationFactory();
+ return (ProducerConfiguration)unmarshaller.unmarshal(inputStream, factory, null);
+ }
+
+ public void reloadConfiguration() throws Exception
+ {
+ throw new UnsupportedOperationException("reloadConfiguration is not
supported!");
+ }
+
+ public void saveConfiguration() throws Exception
+ {
+ throw new UnsupportedOperationException("saveConfiguration is not
supported!");
+ }
+}