jetspeedurlmanagerservice.java

来自「jetspeed源代码」· Java 代码 · 共 466 行 · 第 1/2 页

JAVA
466
字号
/*
 * Copyright 2000-2001,2004 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.jetspeed.services.urlmanager;

// Java classes
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

// Turbine classes
import org.apache.turbine.services.TurbineBaseService;

// Velocity classes
import org.apache.velocity.runtime.configuration.Configuration;

// Jetspeed classes
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.jetspeed.services.resources.JetspeedResources;

/**
 * <p>This implementation of the URLManagerService is backed by a simple
 * map persisted on disk in a properties file</p>
 * Added: Support for proxies. <br>
 * Example: (Set in <code>JetspeedResources.properties</code>)<br>
 * <code>services.URLManager.proxy.http.host=myproxy.mydomain</code><br>
 * <code>services.URLManager.proxy.http.port=81</code><br>
 *
 * @see URLManagerService
 * @author <a href="mailto:raphael@apache.org">Rapha雔 Luta</a>
 * @author <a href="mailto:sgala@hisitech.com">Santiago Gala</a>
 * @version $Id: JetspeedURLManagerService.java,v 1.16 2004/02/23 03:30:47 jford Exp $
 */
public class JetspeedURLManagerService extends TurbineBaseService implements URLManagerService 
{
    /**
     * Static initialization of the logger for this class
     */    
    private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedURLManagerService.class.getName());
    
    /**
    Map used to store all URL Information.
    */
    private Map urls = new HashMap();

    /**
    Path to the properties file used for persisting the data
    */
    private String path = null;

    /**
    Hashtable to store proxy configuration in
    */
    private Hashtable proxies = new Hashtable();

    /**
     * Late init. Don't return control until early init says we're done.
     */
    public void init( )
    {
        while( !getInit() ) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ie ) {
                logger.info("URLManager service: Waiting for init()..." );
            }
        }

    }



    /**
     * Called during Turbine.init()
     *
     * @param config A ServletConfig.
     */
    public synchronized void init( ServletConfig config )
    {
        //We have already been initialized...
        if( getInit() ) return;

        try
        {
            logger.info ( "JetspeedURLManagerService early init()....starting!");

        // Proxy Settings are stored as 'services.URLManager.proxy.<protocol>.port' and as
        // 'services.URLManager.proxy.<protocol>.port' in JetspeedResource.properties.
        // Get a list of settings and store them in the hashtable
            String prefix = "services." + URLManagerService.SERVICE_NAME + ".proxy.";
            Iterator resourceKeys = JetspeedResources.getKeys( prefix );

            String key, hashKey;
            Object hashValue = null;
            while( resourceKeys.hasNext() )
            {
                key = (String)resourceKeys.next();
                hashKey = key.substring(prefix.length()).toLowerCase();
                if ( hashKey.endsWith(".host") )
                {
                    hashValue = JetspeedResources.getString(key);
                    proxies.put( hashKey, hashValue );
                }
                else if ( hashKey.endsWith(".port") )
                {
                    hashValue = new Integer(JetspeedResources.getInt(key));
                    proxies.put( hashKey, hashValue );
                }
            }

            path = JetspeedResources.getString( "services."+URLManagerService.SERVICE_NAME+".url" );

            if ( path == null) 
            {
                String tempdir = new String("WEB-INF/conf/datasources.properties");
                String ps = System.getProperty("file.separator");

                try
                {
                    ServletContext sc = config.getServletContext();
                    tempdir = sc.getAttribute("javax.servlet.context.tempdir").toString()
                                           + ps + "jetspeed" 
                                           + ps + "conf"
                                           + ps + "datasources.properties";
                    logger.debug("URLMangler: will create file in servlet temp directory " + tempdir);
                }
                catch (Exception e)
                {
                    logger.debug("URLMangler: problems creating file in servlet temp directory "
                           + " falling back to WEB-INF/conf : " + e);
                }     
                path = tempdir;
            }
            else
            {
                logger.debug("URLMangler: will create file in user configured " + path);
                path = config.getServletContext().getRealPath(path);
                // should test for writability here and fallback to servlet tmp directory on failure
            }

            load();
            logger.info ( "JetspeedURLManagerService early init()....finished!");
        }
        catch (Throwable t)
        {
            logger.error ( "Cannot initialize JetspeedURLManagerService!", t );
        }
        setInit(true);

    }

    /**
     * Called during Turbine destroy(). Persist the Manager state
     * to disk
     */
    public void shutdown() {
        save();
    }

    /**
     * Registers a new URL record. If the url is already registered in 
     * the system, doesn't modify the current record.
     * 
     * @param url the url to register
     */
    public void register( String url ) {
        if ( url != null ) {
            URLInfo info = getInfo( url );
            if ( info == null ) {
                register( new URLInfo( url, URLManagerService.STATUS_OK ) );
            }
        }
    }

    /**
     * Registers a new URL record. If the url is already registered in 
     * the system, updates the status of this URL info record
     * 
     * @param url the url to register
     * @param status the status of this url
     */
    public void register( String url, int status ) {
        if ( url != null ) {
            URLInfo info = getInfo( url );
            if ( info == null ) {
                register( new URLInfo( url, status ) );
            } else {
                info.setStatus( status );
            }
        }
    }

    /**
     * Registers a new URL record. If the url is already registered in 
     * the system, updates both the status and the message of this URL 
     * info record
     * 
     * @param url the url to register
     * @param status the status of this url
     * @param message a descriptive message of the status
     */
    public void register( String url, int status, String message ) {
        if ( url != null ) {
            URLInfo info = getInfo( url );
            if ( info == null ) {
                register( new URLInfo( url, status, message ) );
            } else {
                info.setStatus( status );
                info.setMessage( message );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?