You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by we...@apache.org on 2004/02/16 14:29:59 UTC

cvs commit: jakarta-jetspeed-2/services/registry/src/java/org/apache/jetspeed/registry/pico/impl OMFactoryComponentImpl.java PortletRegistryComponent.java

weaver      2004/02/16 05:29:59

  Added:       services/registry/src/java/org/apache/jetspeed/registry/pico/impl
                        OMFactoryComponentImpl.java
                        PortletRegistryComponent.java
  Log:
  component refactoring
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed-2/services/registry/src/java/org/apache/jetspeed/registry/pico/impl/OMFactoryComponentImpl.java
  
  Index: OMFactoryComponentImpl.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.jetspeed.registry.pico.impl;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import org.apache.commons.configuration.Configuration;
  import org.apache.jetspeed.registry.OMFactory;
  
  /**
   * <p>
   * OMFactoryComponentImpl
   * </p>
   * 
   * 
   * @
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   * @version $ $
   *
   */
  public class OMFactoryComponentImpl implements OMFactory
  {
      private Configuration conf;
  
      private Map classMap;
  
      public OMFactoryComponentImpl(Configuration conf)
      {
          this.conf = conf;
          this.classMap = new HashMap();
      }
  
      /** 
       * <p>
       * newInstance
       * </p>
       * 
       * @see org.apache.jetspeed.registry.OMFactory#newInstance(java.lang.Class)
       * @param interfase
       * @return
       */
      public Object newInstance(Class interfase) throws InstantiationException, IllegalAccessException, ClassNotFoundException
      {        
          return newInstance(interfase.getName());
      }
  
      /** 
       * <p>
       * newInstance
       * </p>
       * 
       * @see org.apache.jetspeed.registry.OMFactory#newInstance(java.lang.String)
       * @param key
       * @return
       */
      public Object newInstance(String key) throws InstantiationException, IllegalAccessException, ClassNotFoundException 
      {        
          return getImplementation(key).newInstance();
      }
  
      /** 
       * <p>
       * getImplementation
       * </p>
       * 
       * @see org.apache.jetspeed.registry.OMFactory#getImplementation(java.lang.Class)
       * @param interfase
       * @return
       */
      public Class getImplementation(Class interfase) throws ClassNotFoundException
      {        
          return getImplementation(interfase.getName());
      }
  
      /** 
       * <p>
       * getImplementation
       * </p>
       * 
       * @see org.apache.jetspeed.registry.OMFactory#getImplementation(java.lang.String)
       * @param key
       * @return
       */
      public Class getImplementation(String key) throws ClassNotFoundException
      {
      	if(classMap.containsKey(key))
      	{
      		return (Class) classMap.get(key);
      	}
      	
          String className = conf.getString(key);
  		
          if (className != null)
          {
              Class clazz = Class.forName(className);
              classMap.put(key, clazz);
              return clazz;
          }
          else
          {
              throw new ClassNotFoundException("There is no class defined for the key/interface: " + key);
          }
  
      }
  
  }
  
  
  
  1.1                  jakarta-jetspeed-2/services/registry/src/java/org/apache/jetspeed/registry/pico/impl/PortletRegistryComponent.java
  
  Index: PortletRegistryComponent.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Jetspeed" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Jetspeed", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.jetspeed.registry.pico.impl;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.List;
  import java.util.Locale;
  
  import org.apache.commons.configuration.Configuration;
  import org.apache.jetspeed.exception.RegistryException;
  import org.apache.jetspeed.om.common.MutableLanguage;
  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
  import org.apache.jetspeed.persistence.store.Filter;
  import org.apache.jetspeed.persistence.store.PersistenceStore;
  import org.apache.jetspeed.persistence.store.PersistenceStoreContainer;
  import org.apache.jetspeed.persistence.store.impl.LockFailedException;
  import org.apache.jetspeed.registry.OMFactory;
  import org.apache.pluto.om.common.Language;
  import org.apache.pluto.om.common.ObjectID;
  import org.apache.pluto.om.portlet.PortletApplicationDefinition;
  import org.apache.pluto.om.portlet.PortletDefinition;
  
  /**
   * <p>
   * PortletRegistryComponent
   * </p>
   * <p>
   * Component for accessing the Portlet registry.
   * </p>
   * 
   *  <table border="1">
   *    <tr>
   *     <th>Configuration Key</th>
   *     <th>Optional?</th>
   *     <th>Default</th>
   *     <th>Description</th>
   *    </tr>
   *    <tr>
   *     <td>
   *      persistence.store.name
   *     </td>
   *     <td>
   *      true 
   *     </td>
   *     <td>
   *      jetspeed
   *     </td>
   *     <td>
   *      Name of the persistence store that will be  
   *      used for persistence operations.
   *     </td>
   *    </tr>
   *   </table>
   *
  
   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
   * @version $ $
   *
   */
  public class PortletRegistryComponent implements org.apache.jetspeed.registry.PortletRegistryComponent
  {
  
      protected static final String KEY_STORE_NAME = "persistence.store.name";
      private PersistenceStoreContainer storeContainer;
      private Configuration conf;
      private String jetspeedStoreName;
      private Class portletDefClass;
      private Class portletAppClass;
      private OMFactory omFactory;
  
      /**
       * 
       */
      public PortletRegistryComponent(PersistenceStoreContainer storeContainer, Configuration conf, OMFactory omFactory)
          throws RegistryException
      {
          if (storeContainer == null)
          {
              throw new IllegalArgumentException("storeContainer cannot be null for PortletRegistryComponent");
          }
  
          if (conf == null)
          {
              throw new IllegalArgumentException("conf cannot be null for PortletRegistryComponent");
          }
  
          if (omFactory == null)
          {
              throw new IllegalArgumentException("omFactory cannot be null for PortletRegistryComponent");
          }
  
          this.storeContainer = storeContainer;
          this.conf = conf;
          this.omFactory = omFactory;
          jetspeedStoreName = conf.getString(KEY_STORE_NAME, "jetspeed");
  
          try
          {
              portletDefClass = omFactory.getImplementation(PortletDefinition.class);
              portletAppClass = omFactory.getImplementation(PortletApplicationDefinition.class);
          }
          catch (ClassNotFoundException e)
          {
              throw new RegistryException("Unable to identify implementation classes " + e.toString(), e);
          }
      }
  
      /** 
       * <p>
       * createLanguage
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#createLanguage(java.util.Locale, java.lang.String, java.lang.String, java.lang.String, java.util.Collection)
       * @param locale
       * @param title
       * @param shortTitle
       * @param description
       * @param keywords
       * @return
       * @throws RegistryException
       */
      public Language createLanguage(Locale locale, String title, String shortTitle, String description, Collection keywords)
          throws RegistryException
      {
  
          try
          {
              MutableLanguage lc = (MutableLanguage) omFactory.newInstance(Language.class);
              lc.setLocale(locale);
              lc.setTitle(title);
              lc.setShortTitle(shortTitle);
              lc.setKeywords(keywords);
  
              return lc;
          }
          catch (Exception e)
          {
              throw new RegistryException("Unable to create language object.");
          }
  
      }
  
      /** 
       * <p>
       * getAllPortletDefinitions
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#getAllPortletDefinitions()
       * @return
       */
      public List getAllPortletDefinitions()
      {
          PersistenceStore store = getPersistenceStore();
          prepareTransaction(store);
          return new ArrayList(store.getExtent(portletDefClass));
      }
  
      protected PersistenceStore getPersistenceStore()
      {
          return storeContainer.getStoreForThread(jetspeedStoreName);
      }
  
      /** 
       * <p>
       * getPortletApplication
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#getPortletApplication(org.apache.pluto.om.common.ObjectID)
       * @param id
       * @return
       */
      public MutablePortletApplication getPortletApplication(ObjectID id)
      {
          PersistenceStore store = getPersistenceStore();
          prepareTransaction(store);
          Filter filter = store.newFilter();
          filter.addEqualTo("id", id);
          Object query = store.newQuery(portletAppClass, filter);
          return (MutablePortletApplication) store.getObjectByQuery(query);
      }
  
      private void prepareTransaction(PersistenceStore store)
      {
          if (!store.getTransaction().isOpen())
          {
              store.getTransaction().begin();
          }
      }
  
      /** 
       * <p>
       * getPortletApplication
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#getPortletApplication(java.lang.String)
       * @param name
       * @return
       */
      public MutablePortletApplication getPortletApplication(String name)
      {
          PersistenceStore store = getPersistenceStore();
          prepareTransaction(store);
          Filter filter = store.newFilter();
          filter.addEqualTo("name", name);
          Object query = store.newQuery(portletAppClass, filter);
          return (MutablePortletApplication) store.getObjectByQuery(query);
      }
  
      /** 
       * <p>
       * getPortletApplicationByIndetifier
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#getPortletApplicationByIndetifier(java.lang.String)
       * @param ident
       * @return
       */
      public MutablePortletApplication getPortletApplicationByIndetifier(String ident)
      {
          PersistenceStore store = getPersistenceStore();
          prepareTransaction(store);
          Filter filter = store.newFilter();
          filter.addEqualTo("applicationIdentifier", ident);
          Object query = store.newQuery(portletAppClass, filter);
          return (MutablePortletApplication) store.getObjectByQuery(query);
      }
  
      /** 
       * <p>
       * getPortletApplications
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#getPortletApplications()
       * @return
       */
      public List getPortletApplications()
      {
          PersistenceStore store = getPersistenceStore();
          prepareTransaction(store);
          return new ArrayList(store.getExtent(portletAppClass));
      }
  
      /** 
       * <p>
       * getPortletDefinitionByIndetifier
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#getPortletDefinitionByIndetifier(java.lang.String)
       * @param ident
       * @return
       */
      public PortletDefinitionComposite getPortletDefinitionByIndetifier(String ident)
      {
          PersistenceStore store = getPersistenceStore();
          prepareTransaction(store);
          Filter filter = store.newFilter();
          filter.addEqualTo("portletIdentifier", ident);
          Object query = store.newQuery(portletDefClass, filter);
          return (PortletDefinitionComposite) store.getObjectByQuery(query);
      }
  
      /** 
       * <p>
       * getPortletDefinitionByUniqueName
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#getPortletDefinitionByUniqueName(java.lang.String)
       * @param name
       * @return
       */
      public PortletDefinitionComposite getPortletDefinitionByUniqueName(String name)
      {
          PersistenceStore store = getPersistenceStore();
          prepareTransaction(store);
  
          //parse out names
          int split = name.indexOf("::");
          if (split < 1)
          {
              throw new IllegalArgumentException(
                  "The unique portlet name, \"" + name + "\";  is not well formed.  No \"::\" delimiter was found.");
          }
  
          String appName = name.substring(0, split);
          String portletName = name.substring((split + 2), name.length());
  
          // build filter
          Filter filter = store.newFilter();
          filter.addEqualTo("app.name", appName);
          filter.addEqualTo("name", portletName);
          Object query = store.newQuery(portletDefClass, filter);
          PortletDefinitionComposite pdc = (PortletDefinitionComposite) store.getObjectByQuery(query);
  
          return pdc;
      }
  
      /** 
       * <p>
       * portletApplicationExists
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#portletApplicationExists(java.lang.String)
       * @param appIentity
       * @return
       */
      public boolean portletApplicationExists(String appIentity)
      {
          return getPortletApplicationByIndetifier(appIentity) != null;
      }
  
      /** 
       * <p>
       * portletDefinitionExists
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#portletDefinitionExists(java.lang.String)
       * @param portletIndentity
       * @return
       */
      public boolean portletDefinitionExists(String portletIndentity)
      {
          return getPortletDefinitionByIndetifier(portletIndentity) != null;
      }
  
      /** 
       * <p>
       * portletDefinitionExists
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#portletDefinitionExists(java.lang.String, org.apache.jetspeed.om.common.portlet.MutablePortletApplication)
       * @param portletName
       * @param app
       * @return
       */
      public boolean portletDefinitionExists(String portletName, MutablePortletApplication app)
      {
  
          return getPortletDefinitionByUniqueName(app.getName() + "::" + portletName) != null;
      }
  
      /** 
       * <p>
       * registerPortletApplication
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#registerPortletApplication(org.apache.pluto.om.portlet.PortletApplicationDefinition)
       * @param newApp
       * @throws RegistryException
       */
      public void registerPortletApplication(PortletApplicationDefinition newApp) throws RegistryException
      {
          PersistenceStore store = getPersistenceStore();
          prepareTransaction(store);
  
          try
          {
              store.makePersistent(newApp);
          }
          catch (LockFailedException e)
          {
              throw new RegistryException("Unable to lock PortletApplicaiton for makePersistent: " + e.toString(), e);
          }
  
      }
  
      /** 
       * <p>
       * removeApplication
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#removeApplication(org.apache.pluto.om.portlet.PortletApplicationDefinition)
       * @param app
       * @throws TransactionStateException
       */
      public void removeApplication(PortletApplicationDefinition app) throws RegistryException
      {
          PersistenceStore store = getPersistenceStore();
          prepareTransaction(store);
  
          try
          {
              store.deletePersistent(app);
          }
          catch (LockFailedException e)
          {
              throw new RegistryException("Unable to lock PortletApplication for deletion: " + e.toString(), e);
  
          }
  
      }
  
      /** 
       * <p>
       * updatePortletApplication
       * </p>
       * 
       * @see org.apache.jetspeed.registry.PortletRegistryComponent#updatePortletApplication(org.apache.pluto.om.portlet.PortletApplicationDefinition)
       * @param app
       * @throws RegistryException
       */
      public void updatePortletApplication(PortletApplicationDefinition app) throws RegistryException
      {
          try
          {
              PersistenceStore store = getPersistenceStore();
              prepareTransaction(store);
              store.lockForWrite(app);
              store.getTransaction().checkpoint();
          }
          catch (LockFailedException e)
          {
              throw new RegistryException("Unable to lock PortletApplicaiton for update: " + e.toString(), e);
          }
  
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org