/*
* $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/NamespaceConfig.java,v 1.15 2001/06/14 00:41:55 remm Exp $
* $Revision: 1.15 $
* $Date: 2001/06/14 00:41:55 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" 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"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.slide.common;
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
import java.io.FileReader;
import java.io.Reader;
import java.io.IOException;
import org.apache.slide.structure.*;
import org.apache.slide.content.*;
import org.apache.slide.lock.*;
import org.apache.slide.util.conf.Configuration;
import org.apache.slide.util.conf.ConfigurationException;
import org.apache.slide.util.Messages;
import org.apache.slide.util.logger.Logger;
/**
* Configuration of the Namespace.
*
* @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
* @version $Revision: 1.15 $
*/
public final class NamespaceConfig {
// -------------------------------------------------------------- Constants
/**
* Base role names.
*/
public static final String NOBODY = "nobody";
public static final String NOBODY_ROLE = ObjectNode.class.getName();
public static final String BASE_ROLE = SubjectNode.class.getName();
public static final String ACTION_ROLE = ActionNode.class.getName();
public static final String LINK_ROLE = LinkNode.class.getName();
// ----------------------------------------------------- Instance Variables
/**
* Default action.
*/
protected ActionNode defaultAction;
/**
* Read object action.
*/
protected ActionNode readObjectAction;
/**
* Create object action. Equivalent to createChildAction.
*/
protected ActionNode createObjectAction;
/**
* Remove object action.
*/
protected ActionNode removeObjectAction;
/**
* Grant permission action.
*/
protected ActionNode grantPermissionAction;
/**
* Revoke permission action.
*/
protected ActionNode revokePermissionAction;
/**
* Read permissions action.
*/
protected ActionNode readPermissionsAction;
/**
* Lock object action.
*/
protected ActionNode lockObjectAction;
/**
* Kill lock action.
*/
protected ActionNode killLockAction;
/**
* Read locks action.
*/
protected ActionNode readLocksAction;
/**
* Create revision metadata action.
*/
protected ActionNode createRevisionMetadataAction;
/**
* Modify revision metadata action.
*/
protected ActionNode modifyRevisionMetadataAction;
/**
* Remove revision metadata action.
*/
protected ActionNode removeRevisionMetadataAction;
/**
* Read revision metadata action.
*/
protected ActionNode readRevisionMetadataAction;
/**
* Read revision content action.
*/
protected ActionNode readRevisionContentAction;
/**
* Create revision content action.
*/
protected ActionNode createRevisionContentAction;
/**
* Modify revision content action.
*/
protected ActionNode modifyRevisionContentAction;
/**
* Remove revision content action.
*/
protected ActionNode removeRevisionContentAction;
/**
* Users path.
*/
protected String usersPath;
/**
* Guest user path.
*/
protected String guestPath;
/**
* File path.
*/
protected String filesPath;
/**
* Additional parameters.
*/
protected Hashtable parameters;
/**
* Role name -> role interface mappings.
* Note : One name is associated to only one interface.
*/
protected Hashtable roleMappings = new Hashtable();
/**
* Role interface -> role name mappings.
* Note : One interface can be associated to multiple names, in which case
* only one is stored here.
*/
protected Hashtable roleClassMappings = new Hashtable();
/**
* List of default properties.
* Role name -> Vector[NodeProperty].
*/
protected Hashtable defaultProperties;
/**
* Empty vector. Don't add stuff there.
*/
protected static Vector emptyVector = new Vector();
/**
* Content interceptors.
*/
protected ContentInterceptor[] contentInterceptors
= new ContentInterceptor[0];
/**
* Automatically create users.
*/
protected boolean autoCreateUsers = false;
/**
* Roles implementation to be used for automatically created users.
*/
protected String autoCreateUsersRole = "slideroles.basic.UserRoleImpl";
// ------------------------------------------------------------- Properties
/**
* Default action accessor.
*
* @return ActionNode Default action
*/
public ActionNode getDefaultAction() {
return defaultAction;
}
/**
* Read object action accessor.
*
* @return ActionNode Read object action
*/
public ActionNode getReadObjectAction() {
return readObjectAction;
}
/**
* Create object action accessor.
*
* @return ActionNode Create object action
*/
public ActionNode getCreateObjectAction() {
return createObjectAction;
}
/**
* Remove object accessor.
*
* @return ActionNode Remove object action
*/
public ActionNode getRemoveObjectAction() {
return removeObjectAction;
}
/**
* Grant permission accessor.
*
* @return ActionNode Grant permission action
*/
public ActionNode getGrantPermissionAction() {
return grantPermissionAction;
}
/**
* Revoke permission accessor.
*
* @return ActionNode Revoke permission action
*/
public ActionNode getRevokePermissionAction() {
return revokePermissionAction;
}
/**
* Read permissions accessor.
*
* @return ActionNode Read permissions action
*/
public ActionNode getReadPermissionsAction() {
return readPermissionsAction;
}
/**
* Lock object accessor.
*
* @return ActionNode Lock object action
*/
public ActionNode getLockObjectAction() {
return lockObjectAction;
}
/**
* Kill lock accessor.
*
* @return ActionNode Kill lock action
*/
public ActionNode getKillLockAction() {
return killLockAction;
}
/**
* Read locks accessor.
*
* @return ActionNode Read locks action
*/
public ActionNode getReadLocksAction() {
return readLocksAction;
}
/**
* Create revision metadata accessor.
*
* @return ActionNode Create revision metadata action
*/
public ActionNode getCreateRevisionMetadataAction() {
return createRevisionMetadataAction;
}
/**
* Read revision metadata accessor.
*
* @return ActionNode Read revision metadata action
*/
public ActionNode getReadRevisionMetadataAction() {
return readRevisionMetadataAction;
}
/**
* Modify revision metadata accessor.
*
* @return ActionNode Modify revision metadata action
*/
public ActionNode getModifyRevisionMetadataAction() {
return modifyRevisionMetadataAction;
}
/**
* Remove revision metadata accessor.
*
* @return ActionNode Remove revision metadata action
*/
public ActionNode getRemoveRevisionMetadataAction() {
return removeRevisionMetadataAction;
}
/**
* Read revision content accessor.
*
* @return ActionNode Read revision content action
*/
public ActionNode getReadRevisionContentAction() {
return readRevisionContentAction;
}
/**
* Create revision content accessor.
*
* @return ActionNode Create revision content action
*/
public ActionNode getCreateRevisionContentAction() {
return createRevisionContentAction;
}
/**
* Modify revision content accessor.
*
* @return ActionNode Modify revision content action
*/
public ActionNode getModifyRevisionContentAction() {
return modifyRevisionContentAction;
}
/**
* Remove revision content accessor.
*
* @return ActionNode Remove revision content action
*/
public ActionNode getRemoveRevisionContentAction() {
return removeRevisionContentAction;
}
/**
* Users path accessor.
*
* @return String Users path
*/
public String getUsersPath() {
return usersPath;
}
/**
* Guest path accessor.
*
* @return String Guest path
*/
public String getGuestPath() {
return guestPath;
}
/**
* Files path accessor.
*
* @return String Files path
*/
public String getFilesPath() {
return filesPath;
}
/**
* Get default properties for a given role.
*
* @return Enumeration of properties
*/
public Enumeration getDefaultProperties(String role) {
Vector result = (Vector) defaultProperties.get(role);
if (result == null)
return emptyVector.elements();
else
return result.elements();
}
/**
* Get content interceptors.
*/
ContentInterceptor[] getContentInterceptors() {
return contentInterceptors;
}
// --------------------------------------------------------- Public Methods
/**
* Get parameter value.
*
* @param name Parameter name
* @return String Parameter value
*/
public String getParameter(String name) {
Object result = parameters.get(name);
if (result==null) {
return null;
} else {
return (String) result;
}
}
/**
* Get role mapping. If the interface name is given, one of the names is
* returned. If the name is given, the interface name is returned.
*
* @param name Role name (or interface name)
* @return String Role interface name (or name)
*/
public String getRoleMapping(String name) {
Object result = roleMappings.get(name);
if (result == null) {
return (String) roleClassMappings.get(name);
} else {
return (String) result;
}
}
/**
* Is automcatic user creation active ?
*/
public boolean isAutoCreateUsers() {
return autoCreateUsers;
}
/**
* Get the class name of the role which will be used to create nodes which
* are automatically created when isAutoCreateUsers() returns true.
*/
public String getAutoCreateUsersRole() {
return autoCreateUsersRole;
}
// -------------------------------------------------------- Package Methods
/**
* Initialize the Namespace configuration using the given Configuration
* object.
*
* @param namespace Namespace on which we are trying to load the config
* @param config Castor Config object
* @exception InvalidNamespaceConfigurationException Namespace
* configuration is invalid
* @exception SlideException One of the action nodes doesn't exist
*/
void initializeNamespaceConfig(Namespace namespace, Configuration config)
throws InvalidNamespaceConfigurationException, SlideException {
try {
defaultAction =
getActionNode(namespace, config.getConfiguration
("default-action").getValue());
} catch (ConfigurationException e) {
throw new InvalidNamespaceConfigurationException
(namespace, e.getMessage());
}
try {
readObjectAction =
getActionNode(namespace, config.getConfiguration
("read-object").getValue());
} catch (ConfigurationException e) {
readObjectAction = defaultAction;
}
try {
createObjectAction =
getActionNode(namespace, config.getConfiguration
("create-object").getValue());
} catch (ConfigurationException e) {
createObjectAction = defaultAction;
}
try {
removeObjectAction =
getActionNode(namespace, config.getConfiguration
("remove-object").getValue());
} catch (ConfigurationException e) {
removeObjectAction = defaultAction;
}
try {
grantPermissionAction =
getActionNode(namespace, config.getConfiguration
("grant-permission").getValue());
} catch (ConfigurationException e) {
grantPermissionAction = defaultAction;
}
try {
revokePermissionAction =
getActionNode(namespace, config.getConfiguration
("revoke-permission").getValue());
} catch (ConfigurationException e) {
revokePermissionAction = defaultAction;
}
try {
readPermissionsAction =
getActionNode(namespace, config.getConfiguration
("read-permissions").getValue());
} catch (ConfigurationException e) {
readPermissionsAction = defaultAction;
}
try {
lockObjectAction =
getActionNode(namespace, config.getConfiguration
("lock-object").getValue());
} catch (ConfigurationException e) {
lockObjectAction = defaultAction;
}
try {
killLockAction =
getActionNode(namespace, config.getConfiguration
("kill-lock").getValue());
} catch (ConfigurationException e) {
killLockAction = defaultAction;
}
try {
readLocksAction =
getActionNode(namespace, config.getConfiguration
("read-locks").getValue());
} catch (ConfigurationException e) {
readLocksAction = defaultAction;
}
try {
readRevisionMetadataAction =
getActionNode(namespace, config.getConfiguration
("read-revision-metadata").getValue());
} catch (ConfigurationException e) {
readRevisionMetadataAction = defaultAction;
}
try {
createRevisionMetadataAction =
getActionNode(namespace, config.getConfiguration
("create-revision-metadata").getValue());
} catch (ConfigurationException e) {
createRevisionMetadataAction = defaultAction;
}
try {
modifyRevisionMetadataAction =
getActionNode(namespace, config.getConfiguration
("modify-revision-metadata").getValue());
} catch (ConfigurationException e) {
modifyRevisionMetadataAction = defaultAction;
}
try {
removeRevisionMetadataAction =
getActionNode(namespace, config.getConfiguration
("remove-revision-metadata").getValue());
} catch (ConfigurationException e) {
removeRevisionMetadataAction = defaultAction;
}
try {
readRevisionContentAction =
getActionNode(namespace, config.getConfiguration
("read-revision-content").getValue());
} catch (ConfigurationException e) {
readRevisionContentAction = defaultAction;
}
try {
createRevisionContentAction =
getActionNode(namespace, config.getConfiguration
("create-revision-content").getValue());
} catch (ConfigurationException e) {
createRevisionContentAction = defaultAction;
}
try {
modifyRevisionContentAction =
getActionNode(namespace, config.getConfiguration
("modify-revision-content").getValue());
} catch (ConfigurationException e) {
modifyRevisionContentAction = defaultAction;
}
try {
removeRevisionContentAction =
getActionNode(namespace, config.getConfiguration
("remove-revision-content").getValue());
} catch (ConfigurationException e) {
removeRevisionContentAction = defaultAction;
}
try {
usersPath = config.getConfiguration("userspath").getValue();
} catch (ConfigurationException e) {
usersPath = "";
}
try {
guestPath = config.getConfiguration("guestpath").getValue();
} catch (ConfigurationException e) {
guestPath = "";
}
try {
filesPath = config.getConfiguration("filespath").getValue();
} catch (ConfigurationException e) {
filesPath = "";
}
try {
autoCreateUsers = Boolean.valueOf
(config.getConfiguration("auto-create-users").getValue())
.booleanValue();
} catch (ConfigurationException e) {
autoCreateUsers = false;
}
try {
autoCreateUsersRole =
config.getConfiguration("auto-create-users-role").getValue();
} catch (ConfigurationException e) {
}
parameters = new Hashtable();
Enumeration parametersDef = config.getConfigurations("parameter");
try {
while (parametersDef.hasMoreElements()) {
Configuration parameter =
(Configuration) parametersDef.nextElement();
addParameter(parameter.getAttribute("name"),
parameter.getValue());
}
} catch (ConfigurationException e) {
throw new InvalidNamespaceConfigurationException
(namespace, e.getMessage());
}
// Add basic role mappings
addRoleMapping(NOBODY, NOBODY_ROLE);
addRoleMapping(NOBODY, LINK_ROLE);
addRoleMapping(NOBODY, ACTION_ROLE);
// Note : the base role should be the last one.
addRoleMapping(NOBODY, BASE_ROLE);
Enumeration roleMappingsDef = config.getConfigurations("role");
try {
while (roleMappingsDef.hasMoreElements()) {
Configuration roleMappingDef =
(Configuration) roleMappingsDef.nextElement();
addRoleMapping(roleMappingDef.getAttribute("name"),
roleMappingDef.getValue());
}
} catch (ConfigurationException e) {
throw new InvalidNamespaceConfigurationException
(namespace, e.getMessage());
}
defaultProperties = new Hashtable();
Enumeration defaultPropertiesDef =
config.getConfigurations("default-property");
try {
while (defaultPropertiesDef.hasMoreElements()) {
Configuration defaultProperty =
(Configuration) defaultPropertiesDef.nextElement();
String name = defaultProperty.getAttribute("name");
String value = defaultProperty.getAttribute("value", "");
String propertyNamespace = defaultProperty.getAttribute
("namespace", NodeProperty.DEFAULT_NAMESPACE);
String role = defaultProperty.getAttribute("role");
addDefaultProperty(role, name, value, propertyNamespace);
}
} catch (ConfigurationException e) {
throw new InvalidNamespaceConfigurationException
(namespace, e.getMessage());
}
Enumeration contentInteceptorsDef =
config.getConfigurations("content-interceptor");
try {
while (contentInteceptorsDef.hasMoreElements()) {
Configuration contentInterceptorDef =
(Configuration) contentInteceptorsDef.nextElement();
String classname = contentInterceptorDef.getAttribute("class");
try {
Class contentInterceptorClass =
Class.forName(classname);
ContentInterceptor contentInterceptor =
(ContentInterceptor)
contentInterceptorClass.newInstance();
ContentInterceptor[] tempArray =
new ContentInterceptor[contentInterceptors.length + 1];
for (int i = 0; i < contentInterceptors.length; i++) {
tempArray[i] = contentInterceptors[i];
}
tempArray[contentInterceptors.length] = contentInterceptor;
contentInterceptors = tempArray;
} catch (Exception e) {
namespace.getLogger().log
(Messages.format
("org.apache.slide.common.InvalidContentInterceptor",
classname, e.getMessage()), Logger.WARNING);
}
}
} catch (ConfigurationException e) {
throw new InvalidNamespaceConfigurationException
(namespace, e.getMessage());
}
}
/**
* Initialize the Namespace configuration using the given Configuration
* object.
*
* @param namespace Namespace on which we are trying to load the config
* @param config Castor Config object
* @exception InvalidNamespaceConfigurationException Namespace
* configuration is invalid
* @exception SlideException One of the action nodes doesn't exist
*/
void initializeNamespaceParameters
(Namespace namespace, Configuration config)
throws InvalidNamespaceConfigurationException, SlideException {
parameters = new Hashtable();
Enumeration parametersDef = config.getConfigurations("parameter");
try {
while (parametersDef.hasMoreElements()) {
Configuration parameter =
(Configuration) parametersDef.nextElement();
addParameter(parameter.getAttribute("name"),
parameter.getValue());
}
} catch (ConfigurationException e) {
throw new InvalidNamespaceConfigurationException
(namespace, e.getMessage());
}
// Add basic role mappings
addRoleMapping(NOBODY, NOBODY_ROLE);
addRoleMapping(NOBODY, LINK_ROLE);
addRoleMapping(NOBODY, ACTION_ROLE);
// Note : the base role should be the last one.
addRoleMapping(NOBODY, BASE_ROLE);
Enumeration roleMappingsDef = config.getConfigurations("role");
try {
while (roleMappingsDef.hasMoreElements()) {
Configuration roleMappingDef =
(Configuration) roleMappingsDef.nextElement();
addRoleMapping(roleMappingDef.getAttribute("name"),
roleMappingDef.getValue());
}
} catch (ConfigurationException e) {
throw new InvalidNamespaceConfigurationException
(namespace, e.getMessage());
}
defaultProperties = new Hashtable();
Enumeration defaultPropertiesDef =
config.getConfigurations("default-property");
try {
while (defaultPropertiesDef.hasMoreElements()) {
Configuration defaultProperty =
(Configuration) defaultPropertiesDef.nextElement();
String name = defaultProperty.getAttribute("name");
String value = defaultProperty.getAttribute("value", "");
String propertyNamespace = defaultProperty.getAttribute
("namespace", NodeProperty.DEFAULT_NAMESPACE);
String role = defaultProperty.getAttribute("role");
addDefaultProperty(role, name, value, propertyNamespace);
}
} catch (ConfigurationException e) {
throw new InvalidNamespaceConfigurationException
(namespace, e.getMessage());
}
}
/**
* Create a dummy config, used to create the base namespace data.
*
* @param namespace Namespace
* @exception InvalidNamespaceConfigurationException Namespace
* configuration is invalid
*/
void initializeAsDummyConfig(Namespace namespace)
throws InvalidNamespaceConfigurationException {
try {
Uri tempActionUri = namespace.getUri("/tempaction");
defaultAction = (ActionNode) tempActionUri.getStore()
.retrieveObject(tempActionUri);
readObjectAction = defaultAction;
createObjectAction = defaultAction;
removeObjectAction = defaultAction;
grantPermissionAction = defaultAction;
revokePermissionAction = defaultAction;
readPermissionsAction = defaultAction;
lockObjectAction = defaultAction;
killLockAction = defaultAction;
readLocksAction = defaultAction;
readRevisionMetadataAction = defaultAction;
createRevisionMetadataAction = defaultAction;
modifyRevisionMetadataAction = defaultAction;
removeRevisionMetadataAction = defaultAction;
readRevisionContentAction = defaultAction;
createRevisionContentAction = defaultAction;
modifyRevisionContentAction = defaultAction;
removeRevisionContentAction = defaultAction;
usersPath = "";
filesPath = "";
} catch (Exception e) {
throw new InvalidNamespaceConfigurationException
(namespace, e.getMessage());
}
}
// ------------------------------------------------------ Protected Methods
/**
* Add a new parameter.
*
* @param name Parameter name
* @param value Parameter value
*/
protected void addParameter(String name, String value) {
if ((name!=null) && (value!=null)) {
parameters.put(name, value);
}
}
/**
* Add a new default property.
*
* @param role Role
* @param name Property name
* @param value Property value
* @param namespace Property namespace
*/
protected void addDefaultProperty(String role, String name, String value,
String namespace) {
if ((role!=null) && (name!=null) && (value!=null)) {
Vector currentDefaultProperties =
(Vector) defaultProperties.get(role);
if (currentDefaultProperties == null) {
currentDefaultProperties = new Vector();
defaultProperties.put(role, currentDefaultProperties);
}
currentDefaultProperties.addElement
(new NodeProperty(name, value, namespace));
}
}
/**
* Add a new role mapping.
*
* @param name Role mapping name
* @param value Role mapping value
*/
protected void addRoleMapping(String name, String value) {
if ((name!=null) && (value!=null)) {
roleMappings.put(name, value);
roleClassMappings.put(value, name);
}
}
/**
* Retrieve an action node.
*
* @param actionPath Path of the action
* @param ActionNode Action object
*/
protected ActionNode getActionNode(Namespace namespace, String actionPath)
throws InvalidNamespaceConfigurationException, SlideException {
ActionNode result = null;
if (actionPath != null) {
Uri actionUri = namespace.getUri(actionPath);
result = (ActionNode) actionUri.getStore()
.retrieveObject(actionUri);
} else {
if (defaultAction != null) {
result = defaultAction;
} else {
throw new InvalidNamespaceConfigurationException
(namespace, "Needed default action is not defined");
}
}
return result;
}
}