001    package org.apache.jcs.auxiliary.remote.http.server;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.Serializable;
023    import java.util.Properties;
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.apache.jcs.auxiliary.AuxiliaryCacheConfigurator;
028    import org.apache.jcs.auxiliary.remote.http.behavior.IRemoteHttpCacheConstants;
029    import org.apache.jcs.engine.behavior.ICompositeCacheManager;
030    import org.apache.jcs.engine.logging.behavior.ICacheEventLogger;
031    import org.apache.jcs.utils.config.PropertySetter;
032    
033    /** Creates the server. */
034    public class RemoteHttpCacheSeviceFactory
035    {
036        /** The logger */
037        private final static Log log = LogFactory.getLog( RemoteHttpCacheSeviceFactory.class );
038    
039        /**
040         * Configures the attributes and the event logger and constructs a service.
041         * <p>
042         * @param cacheManager
043         * @return RemoteHttpCacheService
044         */
045        public static <K extends Serializable, V extends Serializable> RemoteHttpCacheService<K, V> createRemoteHttpCacheService( ICompositeCacheManager cacheManager )
046        {
047            Properties props = cacheManager.getConfigurationProperties();
048            ICacheEventLogger cacheEventLogger = configureCacheEventLogger( props );
049            RemoteHttpCacheServerAttributes attributes = configureRemoteHttpCacheServerAttributes( props );
050    
051            RemoteHttpCacheService<K, V> service = new RemoteHttpCacheService<K, V>( cacheManager, attributes, cacheEventLogger );
052            if ( log.isInfoEnabled() )
053            {
054                log.info( "Created new RemoteHttpCacheService " + service );
055            }
056            return service;
057        }
058    
059        /**
060         * Tries to get the event logger.
061         * <p>
062         * @param props
063         * @return ICacheEventLogger
064         */
065        protected static ICacheEventLogger configureCacheEventLogger( Properties props )
066        {
067            ICacheEventLogger cacheEventLogger = AuxiliaryCacheConfigurator
068                .parseCacheEventLogger( props, IRemoteHttpCacheConstants.HTTP_CACHE_SERVER_PREFIX );
069    
070            return cacheEventLogger;
071        }
072    
073        /**
074         * Configure.
075         * <p>
076         * jcs.remotehttpcache.serverattributes.ATTRIBUTENAME=ATTRIBUTEVALUE
077         * <p>
078         * @param prop
079         * @return RemoteCacheServerAttributesconfigureRemoteCacheServerAttributes
080         */
081        protected static RemoteHttpCacheServerAttributes configureRemoteHttpCacheServerAttributes( Properties prop )
082        {
083            RemoteHttpCacheServerAttributes rcsa = new RemoteHttpCacheServerAttributes();
084    
085            // configure automatically
086            PropertySetter.setProperties( rcsa, prop,
087                                          IRemoteHttpCacheConstants.HTTP_CACHE_SERVER_ATTRIBUTES_PROPERTY_PREFIX + "." );
088    
089            return rcsa;
090        }
091    }