Moving to root below branch label

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2005-12-08 07:13:07 +00:00
commit e1e6508fec
1095 changed files with 230566 additions and 0 deletions

View File

@@ -0,0 +1,103 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.alfresco.repo.service;
import java.util.Collection;
import org.alfresco.service.ServiceDescriptor;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.QName;
/**
* Service Descriptor.
*
* @author David Caruana
*/
public class BeanServiceDescriptor
implements ServiceDescriptor
{
// Service Name
private QName serviceName;
// Service Description
private String description;
// Service interface class
private Class interfaceClass;
// Supported Store Protocols
Collection<String> protocols = null;
// Supported Stores
Collection<StoreRef> stores = null;
/*package*/ BeanServiceDescriptor(QName serviceName, ServiceDescriptorMetaData metaData, StoreRedirector redirector)
{
this.serviceName = serviceName;
this.interfaceClass = metaData.getInterface();
this.description = metaData.getDescription();
if (redirector != null)
{
protocols = redirector.getSupportedStoreProtocols();
stores = redirector.getSupportedStores();
}
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceDescriptor#getQualifiedName()
*/
public QName getQualifiedName()
{
return serviceName;
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceDescriptor#getDescription()
*/
public String getDescription()
{
return description;
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceDescriptor#getInterface()
*/
public Class getInterface()
{
return interfaceClass;
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceDescriptor#getSupportedStoreProtocols()
*/
public Collection<String> getSupportedStoreProtocols()
{
return protocols;
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.StoreRedirector#getSupportedStores()
*/
public Collection<StoreRef> getSupportedStores()
{
return stores;
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.alfresco.repo.service;
import org.springframework.aop.support.DefaultIntroductionAdvisor;
/**
* Service Descriptor Advisor
*
* @author David Caruana
*/
public class ServiceDescriptorAdvisor extends DefaultIntroductionAdvisor
{
private static final long serialVersionUID = -3327182176681357761L;
/**
* Construct Service Descriptor Advisor
*
* @param namespace service name namespace
* @param description service description
* @param interfaceClass service interface class
*/
public ServiceDescriptorAdvisor(String namespace, String description, Class interfaceClass)
{
super(new ServiceDescriptorMixin(namespace, description, interfaceClass), ServiceDescriptorMetaData.class);
}
}

View File

@@ -0,0 +1,84 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.alfresco.repo.service;
import org.springframework.beans.factory.FactoryBean;
/**
* Factory for creating Service Descriptor Advisors.
*
* @author David Caruana
*/
public class ServiceDescriptorAdvisorFactory implements FactoryBean
{
private String namespace;
private String description;
private Class interfaceClass;
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
public Object getObject() throws Exception
{
return new ServiceDescriptorAdvisor(namespace, description, interfaceClass);
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
*/
public Class getObjectType()
{
// TODO Auto-generated method stub
return ServiceDescriptorAdvisor.class;
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
*/
public boolean isSingleton()
{
// TODO Auto-generated method stub
return false;
}
/**
* @param namespace the service name namespace
*/
public void setNamespace(String namespace)
{
this.namespace = namespace;
}
/**
* @param description the service description
*/
public void setDescription(String description)
{
this.description = description;
}
/**
* @param interfaceClass the service interface class
*/
public void setInterface(Class interfaceClass)
{
this.interfaceClass = interfaceClass;
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.alfresco.repo.service;
/**
* Service Meta Data
*
* @author David Caruana
*/
public interface ServiceDescriptorMetaData
{
/**
* @return the service name namespace
*/
public String getNamespace();
/**
* @return the service description
*/
public String getDescription();
/**
* @return the service interface class
*/
public Class getInterface();
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.alfresco.repo.service;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
/**
* Service Descriptor Mixin.
*
* @author David Caruana
*/
public class ServiceDescriptorMixin extends DelegatingIntroductionInterceptor
implements ServiceDescriptorMetaData
{
private static final long serialVersionUID = -6511459263796802334L;
private String namespace;
private String description;
private Class interfaceClass;
/**
* Construct Service Descriptor Mixin
*
* @param namespace
* @param description
* @param interfaceClass
*/
public ServiceDescriptorMixin(String namespace, String description, Class interfaceClass)
{
this.namespace = namespace;
this.description = description;
this.interfaceClass = interfaceClass;
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceDescriptorMetaData#getNamespace()
*/
public String getNamespace()
{
return namespace;
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceDescriptorMetaData#getDescription()
*/
public String getDescription()
{
return description;
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceDescriptorMetaData#getInterface()
*/
public Class getInterface()
{
return interfaceClass;
}
}

View File

@@ -0,0 +1,307 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.alfresco.repo.service;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.alfresco.service.ServiceDescriptor;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.coci.CheckOutCheckInService;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.lock.LockService;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.CopyService;
import org.alfresco.service.cmr.repository.MimetypeService;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.TemplateService;
import org.alfresco.service.cmr.rule.RuleService;
import org.alfresco.service.cmr.search.CategoryService;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.cmr.view.ExporterService;
import org.alfresco.service.cmr.view.ImporterService;
import org.alfresco.service.descriptor.DescriptorService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
/**
* Implementation of a Service Registry based on the definition of
* Services contained within a Spring Bean Factory.
*
* @author David Caruana
*/
public class ServiceDescriptorRegistry
implements BeanFactoryAware, BeanFactoryPostProcessor, ServiceRegistry
{
// Bean Factory within which the registry lives
private BeanFactory beanFactory = null;
// Service Descriptor map
private Map<QName, BeanServiceDescriptor> descriptors = new HashMap<QName, BeanServiceDescriptor>();
/* (non-Javadoc)
* @see org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
*/
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
{
Map beans = beanFactory.getBeansOfType(ServiceDescriptorMetaData.class);
Iterator iter = beans.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry entry = (Map.Entry)iter.next();
ServiceDescriptorMetaData metaData = (ServiceDescriptorMetaData)entry.getValue();
QName serviceName = QName.createQName(metaData.getNamespace(), (String)entry.getKey());
StoreRedirector redirector = (entry.getValue() instanceof StoreRedirector) ? (StoreRedirector)entry.getValue() : null;
BeanServiceDescriptor serviceDescriptor = new BeanServiceDescriptor(serviceName, metaData, redirector);
descriptors.put(serviceDescriptor.getQualifiedName(), serviceDescriptor);
}
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
*/
public void setBeanFactory(BeanFactory beanFactory) throws BeansException
{
this.beanFactory = beanFactory;
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceRegistry#getServices()
*/
public Collection<QName> getServices()
{
return Collections.unmodifiableSet(descriptors.keySet());
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceRegistry#isServiceProvided(org.alfresco.repo.ref.QName)
*/
public boolean isServiceProvided(QName service)
{
return descriptors.containsKey(service);
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceRegistry#getServiceDescriptor(org.alfresco.repo.ref.QName)
*/
public ServiceDescriptor getServiceDescriptor(QName service)
{
return descriptors.get(service);
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceRegistry#getService(org.alfresco.repo.ref.QName)
*/
public Object getService(QName service)
{
return beanFactory.getBean(service.getLocalName());
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getDescriptorService()
*/
public DescriptorService getDescriptorService()
{
return (DescriptorService)getService(DESCRIPTOR_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceRegistry#getNodeService()
*/
public NodeService getNodeService()
{
return (NodeService)getService(NODE_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceRegistry#getNodeService()
*/
public AuthenticationService getAuthenticationService()
{
return (AuthenticationService)getService(AUTHENTICATION_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceRegistry#getContentService()
*/
public ContentService getContentService()
{
return (ContentService)getService(CONTENT_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getMimetypeService()
*/
public MimetypeService getMimetypeService()
{
return (MimetypeService)getService(MIMETYPE_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceRegistry#getVersionService()
*/
public VersionService getVersionService()
{
return (VersionService)getService(VERSION_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceRegistry#getLockService()
*/
public LockService getLockService()
{
return (LockService)getService(LOCK_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.ServiceRegistry#getDictionaryService()
*/
public DictionaryService getDictionaryService()
{
return (DictionaryService)getService(DICTIONARY_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getSearchService()
*/
public SearchService getSearchService()
{
return (SearchService)getService(SEARCH_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getTransactionService()
*/
public TransactionService getTransactionService()
{
return (TransactionService)getService(TRANSACTION_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getCopyService()
*/
public CopyService getCopyService()
{
return (CopyService)getService(COPY_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getCheckOutCheckInService()
*/
public CheckOutCheckInService getCheckOutCheckInService()
{
return (CheckOutCheckInService)getService(COCI_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getCategoryService()
*/
public CategoryService getCategoryService()
{
return (CategoryService)getService(CATEGORY_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getNamespaceService()
*/
public NamespaceService getNamespaceService()
{
return (NamespaceService)getService(NAMESPACE_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getImporterService()
*/
public ImporterService getImporterService()
{
return (ImporterService)getService(IMPORTER_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getExporterService()
*/
public ExporterService getExporterService()
{
return (ExporterService)getService(EXPORTER_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getRuleService()
*/
public RuleService getRuleService()
{
return (RuleService)getService(RULE_SERVICE);
}
/*
* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getActionService()
*/
public ActionService getActionService()
{
return (ActionService)getService(ACTION_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getPermissionService()
*/
public PermissionService getPermissionService()
{
return (PermissionService)getService(PERMISSIONS_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getAuthorityService()
*/
public AuthorityService getAuthorityService()
{
return (AuthorityService)getService(AUTHORITY_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getTemplateService()
*/
public TemplateService getTemplateService()
{
return (TemplateService)getService(TEMPLATE_SERVICE);
}
/* (non-Javadoc)
* @see org.alfresco.service.ServiceRegistry#getTemplateService()
*/
public FileFolderService getFileFolderService()
{
return (FileFolderService) getService(FILE_FOLDER_SERVICE);
}
}

View File

@@ -0,0 +1,188 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.alfresco.repo.service;
import java.util.Collection;
import javax.transaction.UserTransaction;
import junit.framework.TestCase;
import org.alfresco.service.ServiceDescriptor;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.coci.CheckOutCheckInService;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.lock.LockService;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.CopyService;
import org.alfresco.service.cmr.repository.MimetypeService;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceDescriptorRegistryTest extends TestCase
{
private ApplicationContext factory = null;
private static String TEST_NAMESPACE = "http://www.alfresco.org/test/serviceregistrytest";
private static QName invalidService = QName.createQName(TEST_NAMESPACE, "invalid");
private static QName service1 = QName.createQName(TEST_NAMESPACE, "service1");
private static QName service2 = QName.createQName(TEST_NAMESPACE, "service2");
private static QName service3 = QName.createQName(TEST_NAMESPACE, "service3");
public void setUp()
{
factory = new ClassPathXmlApplicationContext("org/alfresco/repo/service/testregistry.xml");
}
public void testDescriptor()
{
ServiceRegistry registry = (ServiceRegistry)factory.getBean("serviceRegistry");
Collection services = registry.getServices();
assertNotNull(services);
assertEquals(3, services.size());
assertTrue(registry.isServiceProvided(service1));
assertFalse(registry.isServiceProvided(invalidService));
ServiceDescriptor invalid = registry.getServiceDescriptor(invalidService);
assertNull(invalid);
ServiceDescriptor desc1 = registry.getServiceDescriptor(service1);
assertNotNull(desc1);
assertEquals(service1, desc1.getQualifiedName());
assertEquals("Test Service 1", desc1.getDescription());
assertEquals(TestServiceInterface.class, desc1.getInterface());
ServiceDescriptor desc2 = registry.getServiceDescriptor(service2);
assertNotNull(desc2);
assertEquals(service2, desc2.getQualifiedName());
assertEquals("Test Service 2", desc2.getDescription());
assertEquals(TestServiceInterface.class, desc2.getInterface());
}
public void testService()
{
ServiceRegistry registry = (ServiceRegistry)factory.getBean("serviceRegistry");
TestServiceInterface theService1 = (TestServiceInterface)registry.getService(service1);
assertNotNull(service1);
assertEquals("Test1:service1", theService1.test("service1"));
TestServiceInterface theService2 = (TestServiceInterface)registry.getService(service2);
assertNotNull(service2);
assertEquals("Test2:service2", theService2.test("service2"));
}
public void testStores()
{
ServiceRegistry registry = (ServiceRegistry)factory.getBean("serviceRegistry");
ServiceDescriptor desc3 = registry.getServiceDescriptor(service3);
assertNotNull(desc3);
StoreRedirector theService3 = (StoreRedirector)registry.getService(service3);
assertNotNull(service3);
Collection<String> descStores = desc3.getSupportedStoreProtocols();
assertTrue(descStores.contains("Type1"));
assertTrue(descStores.contains("Type2"));
assertFalse(descStores.contains("Invalid"));
Collection<String> serviceStores = theService3.getSupportedStoreProtocols();
for (String store: descStores)
{
assertTrue(serviceStores.contains(store));
}
}
public void testAppContext()
{
ApplicationContext appContext = new ClassPathXmlApplicationContext("alfresco/application-context.xml");
ServiceRegistry registry = (ServiceRegistry)appContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
assertNotNull(registry);
NodeService s1 = registry.getNodeService();
assertNotNull(s1);
CheckOutCheckInService s2 = registry.getCheckOutCheckInService();
assertNotNull(s2);
ContentService s3 = registry.getContentService();
assertNotNull(s3);
CopyService s4 = registry.getCopyService();
assertNotNull(s4);
DictionaryService s5 = registry.getDictionaryService();
assertNotNull(s5);
LockService s6 = registry.getLockService();
assertNotNull(s6);
MimetypeService s7 = registry.getMimetypeService();
assertNotNull(s7);
SearchService s8 = registry.getSearchService();
assertNotNull(s8);
TransactionService transactionService = registry.getTransactionService();
UserTransaction s9 = transactionService.getUserTransaction();
assertNotNull(s9);
UserTransaction s10 = transactionService.getUserTransaction();
assertNotNull(s10);
assertFalse(s9.equals(s10));
VersionService s11 = registry.getVersionService();
assertNotNull(s11);
}
public interface TestServiceInterface
{
public String test(String arg);
}
public static abstract class Component implements TestServiceInterface
{
private String type;
private Component(String type)
{
this.type = type;
}
public String test(String arg)
{
return type + ":" + arg;
}
}
public static class Test1Component extends Component
{
private Test1Component()
{
super("Test1");
}
}
public static class Test2Component extends Component
{
private Test2Component()
{
super("Test2");
}
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.alfresco.repo.service;
import java.util.Collection;
import org.alfresco.service.cmr.repository.StoreRef;
public interface StoreRedirector
{
/**
* @return the names of the protocols supported
*/
public Collection<String> getSupportedStoreProtocols();
/**
* @return the Store Refs of the stores supported
*/
public Collection<StoreRef> getSupportedStores();
}

View File

@@ -0,0 +1,282 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.alfresco.repo.service;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.service.ServiceException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.util.ParameterCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
/**
* This factory provides component redirection based on Store or Node References
* passed into the component.
*
* Redirection is driven by StoreRef and NodeRef parameters. If none are given
* in the method call, the default component is called. Otherwise, the store
* type is extracted from these parameters and the appropriate component called
* for the store type.
*
* An error is thrown if multiple store types are found.
*
* @author David Caruana
*
* @param <I> The component interface class
*/
public class StoreRedirectorProxyFactory<I> implements FactoryBean, InitializingBean
{
// Logger
private static final Log logger = LogFactory.getLog(StoreRedirectorProxyFactory.class);
// The component interface class
private Class<I> proxyInterface = null;
// The default component binding
private I defaultBinding = null;
// The map of store types to component bindings
private Map<String, I> redirectedProtocolBindings = null;
// the map if more specific store Refs to component bindings
private Map<StoreRef, I> redirectedStoreBindings = null;
// The proxy responsible for redirection based on store type
private I redirectorProxy = null;
/**
* Sets the proxy interface
*
* @param proxyInterface
* the proxy interface
*/
public void setProxyInterface(Class<I> proxyInterface)
{
this.proxyInterface = proxyInterface;
}
/**
* Sets the default component binding
*
* @param binding
* the component to call by default
*/
public void setDefaultBinding(I defaultBinding)
{
this.defaultBinding = defaultBinding;
}
/**
* Sets the binding of store type (protocol string) to component
*
* @param bindings
* the bindings
*/
public void setRedirectedProtocolBindings(Map<String, I> protocolBindings)
{
this.redirectedProtocolBindings = protocolBindings;
}
/**
* Sets the binding of store type (protocol string) to component
*
* @param bindings
* the bindings
*/
public void setRedirectedStoreBindings(Map<String, I> storeBindings)
{
redirectedStoreBindings = new HashMap<StoreRef, I>(storeBindings.size());
for(String ref : storeBindings.keySet())
{
redirectedStoreBindings.put(new StoreRef(ref), storeBindings.get(ref));
}
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws ServiceException
{
ParameterCheck.mandatory("Proxy Interface", proxyInterface);
ParameterCheck.mandatory("Default Binding", defaultBinding);
// Setup the redirector proxy
this.redirectorProxy = (I)Proxy.newProxyInstance(proxyInterface.getClassLoader(), new Class[] { proxyInterface, StoreRedirector.class }, new RedirectorInvocationHandler());
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
public I getObject()
{
return redirectorProxy;
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
*/
public Class getObjectType()
{
return proxyInterface;
}
/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
*/
public boolean isSingleton()
{
return true;
}
/**
* Invocation handler that redirects based on store type
*/
/* package */class RedirectorInvocationHandler implements InvocationHandler, StoreRedirector
{
/* (non-Javadoc)
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
// Handle StoreRedirector Interface
if (method.getDeclaringClass().equals(StoreRedirector.class))
{
return method.invoke(this, args);
}
// Otherwise, determine the apropriate implementation to invoke for
// the service interface method
Object binding = null;
StoreRef storeRef = getStoreRef(args);
if (storeRef == null)
{
binding = StoreRedirectorProxyFactory.this.defaultBinding;
}
else
{
if (StoreRedirectorProxyFactory.this.redirectedStoreBindings != null)
{
binding = StoreRedirectorProxyFactory.this.redirectedStoreBindings.get(storeRef);
}
if ((binding == null) && (StoreRedirectorProxyFactory.this.redirectedProtocolBindings != null))
{
binding = StoreRedirectorProxyFactory.this.redirectedProtocolBindings.get(storeRef.getProtocol());
}
if (binding == null)
{
binding = StoreRedirectorProxyFactory.this.defaultBinding;
}
if (binding == null)
{
throw new ServiceException("Store type " + storeRef + " is not supported");
}
}
if (logger.isDebugEnabled())
logger.debug("Redirecting method " + method + " based on store type " + storeRef);
try
{
// Invoke the appropriate binding
return method.invoke(binding, args);
}
catch (InvocationTargetException e)
{
throw e.getCause();
}
}
/**
* Determine store type from array of method arguments
*
* @param args the method arguments
* @return the store type (or null, if one is not specified)
*/
private StoreRef getStoreRef(Object[] args)
{
StoreRef storeRef = null;
if(args == null)
{
return null;
}
for (Object arg : args)
{
// Extract store type from argument, if store type provided
StoreRef argStoreRef = null;
if (arg instanceof NodeRef)
{
argStoreRef = ((NodeRef) arg).getStoreRef();
}
else if (arg instanceof StoreRef)
{
argStoreRef = ((StoreRef) arg);
}
// Only allow one store type
if (argStoreRef != null)
{
if (storeRef != null && !storeRef.equals(argStoreRef))
{
throw new ServiceException("Multiple store types are not supported - types " + storeRef + " and " + argStoreRef + " passed");
}
storeRef = argStoreRef;
}
}
return storeRef;
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.StoreRedirector#getSupportedStoreProtocols()
*/
public Collection<String> getSupportedStoreProtocols()
{
return Collections.unmodifiableCollection(StoreRedirectorProxyFactory.this.redirectedProtocolBindings.keySet());
}
/* (non-Javadoc)
* @see org.alfresco.repo.service.StoreRedirector#getSupportedStores()
*/
public Collection<StoreRef> getSupportedStores()
{
return Collections.unmodifiableCollection(StoreRedirectorProxyFactory.this.redirectedStoreBindings.keySet());
}
}
}

View File

@@ -0,0 +1,210 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.alfresco.repo.service;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class StoreRedirectorProxyFactoryTest extends TestCase
{
private ApplicationContext factory = null;
public void setUp()
{
factory = new ClassPathXmlApplicationContext("org/alfresco/repo/service/testredirector.xml");
}
public void testRedirect()
{
StoreRef storeRef1 = new StoreRef("Type1", "id");
StoreRef storeRef2 = new StoreRef("Type2", "id");
StoreRef storeRef3 = new StoreRef("Type3", "id");
StoreRef storeRef4 = new StoreRef("Type3", "woof");
NodeRef nodeRef1 = new NodeRef(storeRef1, "id");
NodeRef nodeRef2 = new NodeRef(storeRef2, "id");
TestServiceInterface service = (TestServiceInterface) factory.getBean("redirector_service1");
String result1 = service.defaultBinding("redirector_service1");
assertEquals("Type1:redirector_service1", result1);
String result1a = service.noArgs();
assertEquals("Type1", result1a);
String result2 = service.storeRef(storeRef1);
assertEquals("Type1:" + storeRef1, result2);
String result3 = service.storeRef(storeRef2);
assertEquals("Type2:" + storeRef2, result3);
String result4 = service.nodeRef(nodeRef1);
assertEquals("Type1:" + nodeRef1, result4);
String result5 = service.nodeRef(nodeRef2);
assertEquals("Type2:" + nodeRef2, result5);
String result6 = service.multiStoreRef(storeRef1, storeRef1);
assertEquals("Type1:" + storeRef1 + "," + storeRef1, result6);
String result7 = service.multiStoreRef(storeRef2, storeRef2);
assertEquals("Type2:" + storeRef2 + "," + storeRef2, result7);
String result8 = service.multiNodeRef(nodeRef1, nodeRef1);
assertEquals("Type1:" + nodeRef1 + "," + nodeRef1, result8);
String result9 = service.multiNodeRef(nodeRef2, nodeRef2);
assertEquals("Type2:" + nodeRef2 + "," + nodeRef2, result9);
String result10 = service.mixedStoreNodeRef(storeRef1, nodeRef1);
assertEquals("Type1:" + storeRef1 + "," + nodeRef1, result10);
String result11 = service.mixedStoreNodeRef(storeRef2, nodeRef2);
assertEquals("Type2:" + storeRef2 + "," + nodeRef2, result11);
String result12 = service.mixedStoreNodeRef(null, null);
assertEquals("Type1:null,null", result12);
String result13 = service.mixedStoreNodeRef(storeRef1, null);
assertEquals("Type1:" + storeRef1 + ",null", result13);
// Direct store refs
String result14 = service.storeRef(storeRef3);
assertEquals("Type3:" + storeRef3, result14);
String result15 = service.storeRef(storeRef4);
assertEquals("Type1:" + storeRef4, result15);
}
public void testInvalidArgs()
{
StoreRef defaultRef = new StoreRef("Type1", "id");
StoreRef storeRef1 = new StoreRef("InvalidType1", "id");
NodeRef nodeRef1 = new NodeRef(storeRef1, "id");
TestServiceInterface service = (TestServiceInterface) factory.getBean("redirector_service1");
String result1 = service.storeRef(storeRef1);
assertEquals("Type1:" + storeRef1, result1);
String result2 = service.nodeRef(nodeRef1);
assertEquals("Type1:" + nodeRef1, result2);
}
public void testException()
{
StoreRef storeRef1 = new StoreRef("Type1", "id");
NodeRef nodeRef1 = new NodeRef(storeRef1, "id");
TestServiceInterface service = (TestServiceInterface) factory.getBean("redirector_service1");
try
{
service.throwException(nodeRef1);
fail("Service method did not throw exception");
}
catch(Exception e)
{
assertTrue(e instanceof IllegalArgumentException);
assertEquals(nodeRef1.toString(), e.getMessage());
}
}
public interface TestServiceInterface
{
public String noArgs();
public String defaultBinding(String arg);
public String storeRef(StoreRef ref1);
public String nodeRef(NodeRef ref1);
public String multiStoreRef(StoreRef ref1, StoreRef ref2);
public String multiNodeRef(NodeRef ref1, NodeRef ref2);
public String mixedStoreNodeRef(StoreRef ref2, NodeRef ref1);
public void throwException(NodeRef ref1);
}
public static abstract class Component implements TestServiceInterface
{
private String type;
private Component(String type)
{
this.type = type;
}
public String noArgs()
{
return type;
}
public String defaultBinding(String arg)
{
return type + ":" + arg;
}
public String nodeRef(NodeRef ref1)
{
return type + ":" + ref1;
}
public String storeRef(StoreRef ref1)
{
return type + ":" + ref1;
}
public String multiNodeRef(NodeRef ref1, NodeRef ref2)
{
return type + ":" + ref1 + "," + ref2;
}
public String multiStoreRef(StoreRef ref1, StoreRef ref2)
{
return type + ":" + ref1 + "," + ref2;
}
public String mixedStoreNodeRef(StoreRef ref1, NodeRef ref2)
{
return type + ":" + ref1 + "," + ref2;
}
public void throwException(NodeRef ref1)
{
throw new IllegalArgumentException(ref1.toString());
}
}
public static class Type1Component extends Component
{
private Type1Component()
{
super("Type1");
}
}
public static class Type2Component extends Component
{
private Type2Component()
{
super("Type2");
}
}
public static class Type3Component extends Component
{
private Type3Component()
{
super("Type3");
}
}
}

View File

@@ -0,0 +1,8 @@
<model name="test:serviceregistrytest" xmlns="http://www.alfresco.org/model/dictionary/1.0">
<namespaces>
<namespace uri="http://www.alfresco.org/test/serviceregistrytest" prefix="test"/>
</namespaces>
</model>

View File

@@ -0,0 +1,35 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<bean id="type1_component" class="org.alfresco.repo.service.StoreRedirectorProxyFactoryTest$Type1Component">
</bean>
<bean id="type2_component" class="org.alfresco.repo.service.StoreRedirectorProxyFactoryTest$Type2Component">
</bean>
<bean id="type3_component" class="org.alfresco.repo.service.StoreRedirectorProxyFactoryTest$Type3Component">
</bean>
<bean id="redirector_service1" class="org.alfresco.repo.service.StoreRedirectorProxyFactory">
<property name="proxyInterface">
<value>org.alfresco.repo.service.StoreRedirectorProxyFactoryTest$TestServiceInterface</value>
</property>
<property name="defaultBinding">
<ref bean="type1_component"></ref>
</property>
<property name="redirectedProtocolBindings">
<map>
<entry key="Type1"><ref bean="type1_component"></ref></entry>
<entry key="Type2"><ref bean="type2_component"></ref></entry>
</map>
</property>
<property name="redirectedStoreBindings">
<map>
<entry key="Type3://id"><ref bean="type3_component"></ref></entry>
</map>
</property>
</bean>
</beans>

View File

@@ -0,0 +1,111 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<import resource="testredirector.xml" />
<bean id="namespaceDAO" class="org.alfresco.repo.dictionary.NamespaceDAOImpl"></bean>
<bean id="dictionaryDAO" class="org.alfresco.repo.dictionary.DictionaryDAOImpl">
<constructor-arg index="0">
<ref bean="namespaceDAO" />
</constructor-arg>
</bean>
<bean id="dictionaryBootstrap" class="org.alfresco.repo.dictionary.DictionaryBootstrap" init-method="bootstrap">
<property name="dictionaryDAO"><ref local="dictionaryDAO"/></property>
<property name="models">
<list>
<value>alfresco/model/dictionaryModel.xml</value>
</list>
</property>
</bean>
<bean id="serviceRegistry" class="org.alfresco.repo.service.ServiceDescriptorRegistry"></bean>
<bean id="AlfrescoServiceDescriptor" abstract="true"
class="org.alfresco.repo.service.ServiceDescriptorAdvisorFactory">
<property name="namespace">
<value>http://www.alfresco.org/test/serviceregistrytest</value>
</property>
</bean>
<bean id="service1" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>org.alfresco.repo.service.ServiceDescriptorRegistryTest$TestServiceInterface</value>
</property>
<property name="target">
<ref local="test1Component" />
</property>
<property name="interceptorNames">
<list>
<idref local="service1_descriptor" />
</list>
</property>
</bean>
<bean id="service1_descriptor" parent="AlfrescoServiceDescriptor">
<property name="interface">
<value>org.alfresco.repo.service.ServiceDescriptorRegistryTest$TestServiceInterface</value>
</property>
<property name="description">
<value>Test Service 1</value>
</property>
</bean>
<bean id="service2" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>org.alfresco.repo.service.ServiceDescriptorRegistryTest$TestServiceInterface</value>
</property>
<property name="target">
<ref local="test2Component" />
</property>
<property name="interceptorNames">
<list>
<idref local="service2_descriptor" />
</list>
</property>
</bean>
<bean id="service2_descriptor" parent="AlfrescoServiceDescriptor">
<property name="interface">
<value>org.alfresco.repo.service.ServiceDescriptorRegistryTest$TestServiceInterface</value>
</property>
<property name="description">
<value>Test Service 2</value>
</property>
</bean>
<bean id="service3" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>org.alfresco.repo.service.ServiceDescriptorRegistryTest$TestServiceInterface, org.alfresco.repo.service.StoreRedirector</value>
</property>
<property name="target">
<ref bean="redirector_service1" />
</property>
<property name="interceptorNames">
<list>
<idref local="service3_descriptor" />
</list>
</property>
</bean>
<bean id="service3_descriptor" parent="AlfrescoServiceDescriptor">
<property name="interface">
<value>org.alfresco.repo.service.ServiceDescriptorRegistryTest$TestServiceInterface</value>
</property>
<property name="description">
<value>Test Service 3</value>
</property>
</bean>
<bean id="test1Component" class="org.alfresco.repo.service.ServiceDescriptorRegistryTest$Test1Component"></bean>
<bean id="test2Component" class="org.alfresco.repo.service.ServiceDescriptorRegistryTest$Test2Component"></bean>
</beans>