You are viewing a plain text version of this content. The canonical link for it is here.
Posted to muse-commits@ws.apache.org by da...@apache.org on 2006/07/28 17:09:41 UTC
svn commit: r426552 - in
/webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core:
AbstractCapability.java AbstractFilePersistence.java Capability.java
Messages.properties Persistence.java
Author: danj
Date: Fri Jul 28 08:09:40 2006
New Revision: 426552
URL: http://svn.apache.org/viewvc?rev=426552&view=rev
Log:
Added the interface and getter/setter necessary to allow capabilities to have their own
individual persistence mechanisms, not just the router. The router persistence will only
handle the EPR -> instance entries, not the state of the capabilities.
Added:
webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/AbstractFilePersistence.java
webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Persistence.java
Modified:
webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/AbstractCapability.java
webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Capability.java
webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Messages.properties
Modified: webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/AbstractCapability.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/AbstractCapability.java?rev=426552&r1=426551&r2=426552&view=diff
==============================================================================
--- webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/AbstractCapability.java (original)
+++ webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/AbstractCapability.java Fri Jul 28 08:09:40 2006
@@ -58,12 +58,19 @@
// name-value pairs specified with <init-param/>
//
private Map _parameters = null;
+
+ private Persistence _persistence = null;
//
// The resource implementing the capability
//
private Resource _resource = null;
+ public Collection getActions()
+ {
+ return Collections.unmodifiableSet(_handlersByAction.keySet());
+ }
+
public String getCapabilityURI()
{
return _capabilityURI;
@@ -94,9 +101,9 @@
return (MessageHandler)_handlersByAction.get(action);
}
- public Collection getActions()
+ public Persistence getPersistence()
{
- return Collections.unmodifiableSet(_handlersByAction.keySet());
+ return _persistence;
}
public Resource getResource()
@@ -123,7 +130,14 @@
public void initializeCompleted()
throws SoapFault
{
- // no default action
+ Persistence persistence = getPersistence();
+
+ if (persistence != null)
+ {
+ ResourceManager manager = getResource().getResourceManager();
+ persistence.setResourceManager(manager);
+ persistence.reload();
+ }
}
public void prepareShutdown()
@@ -163,6 +177,11 @@
while (i.hasNext())
setMessageHandler((MessageHandler)i.next());
+ }
+
+ public void setPersistence(Persistence persistence)
+ {
+ _persistence = persistence;
}
public void setResource(Resource resource)
Added: webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/AbstractFilePersistence.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/AbstractFilePersistence.java?rev=426552&view=auto
==============================================================================
--- webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/AbstractFilePersistence.java (added)
+++ webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/AbstractFilePersistence.java Fri Jul 28 08:09:40 2006
@@ -0,0 +1,259 @@
+/*=============================================================================*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *=============================================================================*/
+
+package org.apache.muse.core;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.TreeSet;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import org.apache.muse.util.messages.Messages;
+import org.apache.muse.util.messages.MessagesFactory;
+import org.apache.muse.util.xml.XmlUtils;
+import org.apache.muse.ws.addressing.EndpointReference;
+import org.apache.muse.ws.addressing.soap.SoapFault;
+
+/**
+ *
+ * AbstractFilePersistence is a component that provides generic resource-to-file
+ * utilities without specifying the format of the XML that goes into the files
+ * for each resource type. It can be used to components that wish to save state
+ * to disk on per-resource basis.
+ *
+ * @author Dan Jemiolo (danj)
+ *
+ */
+
+public abstract class AbstractFilePersistence implements Persistence
+{
+ //
+ // Used to look up all exception messages
+ //
+ private static Messages _MESSAGES = MessagesFactory.get(AbstractFilePersistence.class);
+
+ public static final String FILE_PREFIX = "resource-instance-";
+
+ private Map _fileNumbersByEPR = new HashMap();
+
+ private String _location = null;
+
+ private ResourceManager _manager = null;
+
+ //
+ // name-value pairs specified with <init-param/>
+ //
+ private Map _parameters = null;
+
+ protected abstract void createResourceFile(EndpointReference epr, Resource resource, File resourceFile)
+ throws SoapFault;
+
+ protected void createResourceFile(EndpointReference epr, Resource resource)
+ throws SoapFault
+ {
+ int nextNumber = getNextFileNumber();
+ String nextFileName = getNextFileName(nextNumber);
+ File resourceTypeDir = getResourceTypeDirectory(resource.getContextPath());
+ File resourceFile = new File(resourceTypeDir, nextFileName);
+
+ createResourceFile(epr, resource, resourceFile);
+
+ getFileNumbersByEPR().put(epr, new Integer(nextNumber));
+ }
+
+ protected void destroyResourceFile(EndpointReference epr)
+ throws SoapFault
+ {
+ Integer fileNumber = (Integer)getFileNumbersByEPR().get(epr);
+ FileNumberFilter filter = new FileNumberFilter(fileNumber);
+
+ String contextPath = getContextPath(epr);
+ File resourceTypeDir = getResourceTypeDirectory(contextPath);
+ File[] results = resourceTypeDir.listFiles(filter);
+
+ if (results.length == 0)
+ {
+ Object[] filler = { "\n\n" + epr };
+ throw new SoapFault(_MESSAGES.get("NoFileForEPR", filler));
+ }
+
+ results[0].delete();
+ getFileNumbersByEPR().remove(epr);
+ }
+
+ protected String getContextPath(EndpointReference epr)
+ {
+ String addressPath = epr.getAddress().getPath();
+ int slash = addressPath.lastIndexOf('/');
+ return addressPath.substring(slash + 1);
+ }
+
+ protected Integer getFileNumber(String fileName)
+ {
+ int underscore = fileName.lastIndexOf('-');
+ int extension = fileName.lastIndexOf('.');
+ String numberString = fileName.substring(underscore + 1, extension);
+ return new Integer(numberString);
+ }
+
+ protected Map getFileNumbersByEPR()
+ {
+ return _fileNumbersByEPR;
+ }
+
+ public String getInitializationParameter(String name)
+ {
+ return (String)getInitializationParameters().get(name);
+ }
+
+ public Map getInitializationParameters()
+ {
+ return _parameters;
+ }
+
+ protected String getNextFileName(int fileNumber)
+ {
+ return FILE_PREFIX + fileNumber + ".xml";
+ }
+
+ protected int getNextFileNumber()
+ {
+ if (getFileNumbersByEPR().isEmpty())
+ return 1;
+
+ TreeSet sortedNumbers = new TreeSet(getFileNumbersByEPR().values());
+ Integer largest = (Integer)sortedNumbers.last();
+ return largest.intValue() + 1;
+ }
+
+ protected File getPersistenceDirectory()
+ {
+ String path = getPersistenceLocation();
+
+ if (path == null)
+ throw new RuntimeException(_MESSAGES.get("NoPersistenceLocation"));
+
+ File workingDir = getResourceManager().getEnvironment().getRealDirectory();
+ return new File(workingDir, path);
+ }
+
+ public ResourceManager getResourceManager()
+ {
+ return _manager;
+ }
+
+ protected File getResourceTypeDirectory(String contextPath)
+ {
+ File dir = new File(getPersistenceDirectory(), contextPath);
+
+ if (!dir.exists())
+ dir.mkdirs();
+
+ return dir;
+ }
+
+ public void reload()
+ throws SoapFault
+ {
+ Iterator i = getResourceManager().getResourceContextPaths().iterator();
+
+ while (i.hasNext())
+ {
+ String contextPath = (String)i.next();
+ File resourceTypeDir = getResourceTypeDirectory(contextPath);
+ reloadResources(resourceTypeDir);
+ }
+ }
+
+ protected void reloadResources(File resourceTypeDir)
+ throws SoapFault
+ {
+ File[] resourceFiles = resourceTypeDir.listFiles(new ResourceFileFilter());
+
+ for (int n = 0; n < resourceFiles.length; ++n)
+ {
+ Document xmlDoc = null;
+
+ try
+ {
+ xmlDoc = XmlUtils.createDocument(resourceFiles[n]);
+ }
+ catch (Throwable error)
+ {
+ throw new RuntimeException(error.getMessage(), error);
+ }
+
+ Element root = XmlUtils.getFirstElement(xmlDoc);
+ Resource resource = reloadResource(root);
+
+ String fileName = resourceFiles[n].getName();
+ Integer fileNumber = getFileNumber(fileName);
+
+ getFileNumbersByEPR().put(resource.getEndpointReference(), fileNumber);
+ }
+ }
+
+ protected abstract Resource reloadResource(Element resourceXML)
+ throws SoapFault;
+
+ public void setInitializationParameters(Map parameters)
+ {
+ _parameters = parameters;
+ }
+
+ public void setResourceManager(ResourceManager manager)
+ {
+ _manager = manager;
+ }
+
+ protected class FileNumberFilter implements FileFilter
+ {
+ private Integer _fileNumber = null;
+
+ public FileNumberFilter(Integer fileNumber)
+ {
+ _fileNumber = fileNumber;
+ }
+
+ public boolean accept(File file)
+ {
+ return file.getName().indexOf("-" + _fileNumber + ".xml") >= 0;
+ }
+ }
+
+ protected class ResourceFileFilter implements FileFilter
+ {
+ public boolean accept(File file)
+ {
+ return file.getName().startsWith(FILE_PREFIX);
+ }
+ }
+
+ public String getPersistenceLocation()
+ {
+ return _location;
+ }
+
+ public void setPersistenceLocation(String location)
+ {
+ _location = location;
+ }
+}
Modified: webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Capability.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Capability.java?rev=426552&r1=426551&r2=426552&view=diff
==============================================================================
--- webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Capability.java (original)
+++ webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Capability.java Fri Jul 28 08:09:40 2006
@@ -43,6 +43,16 @@
{
/**
*
+ * @return All of the WS-A Action URIs for which the capability has
+ * a MessageHandler
+ *
+ * @see #getMessageHandler(String)
+ *
+ */
+ Collection getActions();
+
+ /**
+ *
* @return The unique URI (within the resource type) that defines this
* capability.
*
@@ -72,15 +82,7 @@
*/
MessageHandler getMessageHandler(String action);
- /**
- *
- * @return All of the WS-A Action URIs for which the capability has
- * a MessageHandler
- *
- * @see #getMessageHandler(String)
- *
- */
- Collection getActions();
+ Persistence getPersistence();
/**
*
@@ -126,7 +128,9 @@
void setLog(Logger log);
- void setMessageHandlers(Collection handlers);
+ void setMessageHandlers(Collection handlers);
+
+ void setPersistence(Persistence persistence);
void setResource(Resource resource);
}
Modified: webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Messages.properties
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Messages.properties?rev=426552&r1=426551&r2=426552&view=diff
==============================================================================
--- webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Messages.properties (original)
+++ webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Messages.properties Fri Jul 28 08:09:40 2006
@@ -2,3 +2,4 @@
ResourceInitialized = The resource at 'XXX' has been initialized.
ResourceDestroyed = The resource at 'XXX' has been destroyed.
ActionNotSupported = The resource at 'XXX' does not expose an operation named 'XXX' through any of its capabilities.
+NoFileForEPR = There is no XML file storing a router entry for the following EPR: XXX
Added: webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Persistence.java
URL: http://svn.apache.org/viewvc/webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Persistence.java?rev=426552&view=auto
==============================================================================
--- webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Persistence.java (added)
+++ webservices/muse/trunk/modules/muse-core/src/org/apache/muse/core/Persistence.java Fri Jul 28 08:09:40 2006
@@ -0,0 +1,33 @@
+/*=============================================================================*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *=============================================================================*/
+
+package org.apache.muse.core;
+
+import org.apache.muse.ws.addressing.soap.SoapFault;
+
+public interface Persistence extends InitializationParameters
+{
+ String getPersistenceLocation();
+
+ ResourceManager getResourceManager();
+
+ void reload()
+ throws SoapFault;
+
+ void setPersistenceLocation(String location);
+
+ void setResourceManager(ResourceManager manager);
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: muse-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: muse-commits-help@ws.apache.org