?? databaseregistryservice.java
字號:
/*
* Copyright 2000-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.registry;
// Java classes
import java.io.Reader;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.servlet.ServletConfig;
//turbine stuff
import org.apache.turbine.services.InitializationException;
import org.apache.turbine.services.TurbineBaseService;
import org.apache.turbine.services.TurbineServices;
import org.apache.turbine.services.resources.ResourceService;
import org.apache.turbine.services.servlet.ServletService;
// Jetspeed classes
import org.apache.jetspeed.om.registry.DBRegistry;
import org.apache.jetspeed.om.registry.Registry;
import org.apache.jetspeed.om.registry.RegistryEntry;
import org.apache.jetspeed.om.registry.RegistryException;
import org.apache.jetspeed.om.registry.base.BaseRegistry;
import org.apache.jetspeed.om.registry.base.LocalRegistry;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
/**
* <p>This is an implementation of the <code>RegistryService</code>
* based on the Jetspeed Database Persistence Manager</p>
*
* @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
* @author <a href="mailto:susinha@cisco.com">Suchisubhra Sinha</a>
* @version $Id: DatabaseRegistryService.java,v 1.6 2004/02/23 03:31:50 jford Exp $
*/
public class DatabaseRegistryService
extends TurbineBaseService
implements RegistryService , FileRegistry
{
private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(CastorRegistryService.class.getName());
/** The name of this service */
public static String SERVICE_NAME = "DatabaseRegistry";
public static final int DEFAULT_VERBOSE = 1;
/** regsitry type keyed list of entries */
private Hashtable registries = new Hashtable();
/** The list of default fragments stores for newly created objects */
private Hashtable defaults = new Hashtable();
/** The Castor generated RegsitryFragment objects */
private Hashtable fragments = new Hashtable();
/** Associates entries with their fragments name for quick lookup */
private Hashtable entryIndex = new Hashtable();
/** the Watcher object which monitors the regsitry directory */
private DatabaseRegistryWatcher watcher = null;
/** Assign the default poolname */
private final static String POOL_NAME = "database";
/** controls amount of debug output, the bigger the more output will be generated */
private int verbose = DEFAULT_VERBOSE;
/** Base class to implement */
private static Hashtable baseClass = new Hashtable();
/**
* Returns a Registry object for further manipulation
*
* @param regName the name of the registry to fetch
* @return a Registry object if found by the manager or null
*/
public Registry get(String regName)
{
return (Registry) registries.get(regName);
}
/**
* List all the registry currently available to this service
*
* @return an Enumeration of registry names.
*/
public Enumeration getNames()
{
return registries.keys();
}
/**
* Creates a new RegistryEntry instance compatible with the current
* Registry instance implementation
*
* @param regName the name of the registry to use
* @return the newly created RegistryEntry
*/
public RegistryEntry createEntry(String regName)
{
RegistryEntry entry = null;
Registry registry = (Registry) registries.get(regName);
if (registry != null)
{
entry = registry.createEntry();
}
return entry;
}
/**
* Returns a RegistryEntry from the named Registry.
* This is a convenience wrapper around {@link
* org.apache.jetspeed.om.registry.Registry#getEntry }
*
* @param regName the name of the registry
* @param entryName the name of the entry to retrieve from the registry.
* @return a RegistryEntry object if the key is found or null
*/
public RegistryEntry getEntry(String regName, String entryName)
{
try
{
return ((Registry) registries.get(regName)).getEntry(entryName);
}
catch (RegistryException e)
{
if (logger.isInfoEnabled())
{
logger.info(
"RegistryService: Failed to retrieve "
+ entryName
+ " from "
+ regName);
}
}
catch (NullPointerException e)
{
logger.error(
"DatabaseRegistryService: "
+ regName
+ " registry is not known ");
logger.error(e);
}
return null;
}
/**
* Add a new RegistryEntry in the named Registry.
* This is a convenience wrapper around {@link
* org.apache.jetspeed.om.registry.Registry#addEntry }
*
* @param regName the name of the registry
* @param entry the Registry entry to add
* @exception Sends a RegistryException if the manager can't add
* the provided entry
*/
public void addEntry(String regName, RegistryEntry entry)
throws RegistryException
{
if (entry == null)
{
return;
}
LocalRegistry registry = (LocalRegistry) registries.get(regName);
if (registry != null)
{
String fragmentName = (String) entryIndex.get(entry.getName());
if (fragmentName == null)
{
// either the entry was deleted or it does not exist
// in both cases, use the default fragment
fragmentName = (String) defaults.get(regName);
}
RegistryFragment fragment =
(RegistryFragment) fragments.get(fragmentName);
//Fragment can be (and sometimes is, but should not be) null
if (fragment == null)
{
fragment = new RegistryFragment();
fragment.put(regName, new Vector());
fragments.put(fragmentName, fragment);
}
else
{
Vector vectRegistry = (Vector) fragment.get(regName);
if (vectRegistry == null)
{
fragment.put(regName, new Vector());
}
}
synchronized (entryIndex)
{
if (registry.hasEntry(entry.getName()))
{
fragment.setEntry(regName, entry);
registry.setLocalEntry(entry);
}
else
{
fragment.addEntry(regName, entry);
registry.addLocalEntry(entry);
}
entryIndex.put(entry.getName(), fragmentName);
// mark this fragment so that it's persisted next time
// the registry watcher is running
fragment.setDirty(true);
}
}
}
/**
* Deletes a RegistryEntry from the named Registry
* This is a convenience wrapper around {@link
* org.apache.jetspeed.om.registry.Registry#removeEntry }
*
* @param regName the name of the registry
* @param entryName the name of the entry to remove
*/
public void removeEntry(String regName, String entryName)
{
if (entryName == null)
{
return;
}
LocalRegistry registry = (LocalRegistry) registries.get(regName);
if (registry != null)
{
String fragmentName = (String) entryIndex.get(entryName);
if (fragmentName != null)
{
RegistryFragment fragment =
(RegistryFragment) fragments.get(fragmentName);
synchronized (entryIndex)
{
fragment.removeEntry(regName, entryName);
entryIndex.remove(entryName);
// mark this fragment so that it's persisted next time
// the registry watcher is running
fragment.setDirty(true);
}
}
// the entry is physically removed, remove the dangling reference
registry.removeLocalEntry(entryName);
}
}
/**
* This is the early initialization method called by the
* Turbine <code>Service</code> framework
*/
public synchronized void init(ServletConfig conf)
throws InitializationException
{
int refreshRate = 0;
Vector names = new Vector();
//Ensure that the servlet service is initialized
TurbineServices.getInstance().initService(ServletService.SERVICE_NAME, conf);
ResourceService serviceConf =
((TurbineServices) TurbineServices.getInstance()).getResources(SERVICE_NAME);
//build the map of default fragments, eahc registry must be associated
//with at least one fragment
try
{
refreshRate = serviceConf.getInt("refreshRate", DEFAULT_REFRESH);
ResourceService defaults = serviceConf.getResources("default");
Iterator i = defaults.getKeys();
while (i.hasNext())
{
String name = (String) i.next();
// add this name in the list of available registries
names.add(name);
try
{
String registryClass =
"org.apache.jetspeed.om.registry.database.BaseJetspeed"
+ name
+ "Peer";
baseClass.put(
name,
(DBRegistry) Class
.forName(registryClass)
.newInstance());
}
catch (Exception e)
{
if (logger.isWarnEnabled())
{
logger.warn(
"DatabaseRegistryService: Class "
+ name
+ " not found");
}
}
}
}
catch (Throwable t)
{
throw new InitializationException("Unable to initialize DatabaseRegistryService, missing config keys");
}
this.watcher = new DatabaseRegistryWatcher();
this.watcher.setSubscriber(this);
if (refreshRate == 0)
{
this.watcher.setDone();
}
else
{
this.watcher.setRefreshRate(refreshRate);
}
// changing the base will trigger a synchronous loading of the fragments
this.watcher.changeBase(names);
//Mark that we are done
setInit(true);
// load the registries
Enumeration en = names.elements();
RegistryService localeService =
(RegistryService) TurbineServices
.getInstance()
.getService(RegistryService.SERVICE_NAME);
while (en.hasMoreElements())
{
String name = (String) en.nextElement();
Registry registry = (Registry) registries.get(name);
if (registry == null)
{
String registryClass = null;
try
{
registry = localeService.get(name);
}
catch (Exception e)
{
if (logger.isWarnEnabled())
{
logger.warn(
"DatabaseRegistryService: Class "
+ registryClass
+ " not found, reverting to default Registry");
}
registry = new BaseRegistry();
}
registries.put(name, registry);
}
refresh(name);
}
// Start the directory watcher thread and rely on its refresh process
// to completely load all registries
if (this.watcher != null)
{
this.watcher.start();
}
if (logger.isDebugEnabled())
{
logger.debug(
"DatabaseRegistryService: early init()....end!, this.getInit()= "
+ getInit());
}
}
/**
* @return a Map of all fragments keyed by file names
*/
public Map getFragmentMap()
{
return (Map) fragments.clone();
}
/** Late init method from Turbine Service model */
public void init() throws InitializationException
{
if (logger.isDebugEnabled())
{
logger.debug("DatabaseRegistryService: Late init called");
}
while (!getInit())
{
//Not yet...
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -