001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.internal;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import javax.portlet.PreferencesValidator;
023: import javax.portlet.ValidatorException;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.pluto.descriptors.portlet.PortletDD;
028:
029: /**
030: * The portlet preferences validator registry. This class caches the portlet
031: * preferences validator instances for portlet definitions, and ensure that
032: * only one validator instance is created per portlet definition.
033: *
034: * @since 2006-02-10
035: */
036: public class PreferencesValidatorRegistry {
037:
038: /** Logger. */
039: private static final Log LOG = LogFactory
040: .getLog(PreferencesValidatorRegistry.class);
041:
042: /** The static singleton registry instance. */
043: private static final PreferencesValidatorRegistry REGISTRY = new PreferencesValidatorRegistry();
044:
045: // Private Member Variables ------------------------------------------------
046:
047: /**
048: * The preferences validator cache: key is the portlet definition, value is
049: * the portlet preferences validator instance.
050: */
051: private final Map cache = new HashMap();
052:
053: // Constructor -------------------------------------------------------------
054:
055: /**
056: * Private constructor that prevents external instantiation.
057: */
058: private PreferencesValidatorRegistry() {
059: // Do nothing.
060: }
061:
062: public static PreferencesValidatorRegistry getRegistry() {
063: return REGISTRY;
064: }
065:
066: // Public Methods ----------------------------------------------------------
067:
068: /**
069: * Returns the preferences validator instance for the given portlet
070: * definition. If no preferences validator class is defined for the portlet
071: * definition, null is returned. This method caches the validator instances
072: * in the cache to ensure that only one validator instance is created per
073: * portlet definition.
074: * @param portletDD the portlet definition.
075: * @return the preferences validator if defined for the portlet definition.
076: * @throw ValidatorException if fail to instantiate validator instance.
077: */
078: public PreferencesValidator getPreferencesValidator(
079: PortletDD portletDD) throws ValidatorException {
080:
081: // Try to retrieve the validator from cache.
082: PreferencesValidator validator = (PreferencesValidator) cache
083: .get(portletDD);
084: if (validator != null) {
085: return validator;
086: }
087:
088: // Try to construct the validator instance for the portlet definition.
089: String className = portletDD.getPortletPreferences()
090: .getPreferencesValidator();
091: if (className != null) {
092: if (LOG.isDebugEnabled()) {
093: LOG.debug("Creating preferences validator: "
094: + className);
095: }
096: ClassLoader loader = Thread.currentThread()
097: .getContextClassLoader();
098: try {
099: Class clazz = loader.loadClass(className);
100: validator = (PreferencesValidator) clazz.newInstance();
101: cache.put(portletDD, validator);
102: } catch (InstantiationException ex) {
103: LOG.error("Error instantiating validator.", ex);
104: throw new ValidatorException(ex, null);
105: } catch (IllegalAccessException ex) {
106: LOG.error("Error instantiating validator.", ex);
107: throw new ValidatorException(ex, null);
108: } catch (ClassNotFoundException ex) {
109: LOG.error("Error instantiating validator.", ex);
110: throw new ValidatorException(ex, null);
111: } catch (ClassCastException ex) {
112: LOG
113: .error(
114: "Error casting instance to PreferencesValidator.",
115: ex);
116: throw new ValidatorException(ex, null);
117: }
118: }
119: return validator;
120: }
121:
122: }
|