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: */
018:
019: package org.apache.lenya.ac.file;
020:
021: import java.io.File;
022:
023: import org.apache.avalon.framework.configuration.Configuration;
024: import org.apache.avalon.framework.configuration.ConfigurationException;
025: import org.apache.avalon.framework.configuration.DefaultConfiguration;
026: import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
027: import org.apache.avalon.framework.logger.Logger;
028: import org.apache.lenya.ac.AccessControlException;
029: import org.apache.lenya.ac.Item;
030: import org.apache.lenya.ac.ItemManager;
031: import org.apache.lenya.ac.impl.AbstractRole;
032: import org.apache.lenya.ac.impl.ItemConfiguration;
033:
034: /**
035: * File-based role implementation.
036: * @version $Id: FileRole.java 597037 2007-11-21 11:15:43Z andreas $
037: */
038: public class FileRole extends AbstractRole implements Item {
039:
040: protected static final String ATTR_ASSIGNABLE = "assignable";
041:
042: /**
043: * Creates a new file role.
044: * @param itemManager The item manager.
045: * @param logger The logger.
046: * @param id The role ID.
047: */
048: public FileRole(ItemManager itemManager, Logger logger, String id) {
049: this (itemManager, logger);
050: setId(id);
051: }
052:
053: protected static final String ROLE = "role";
054:
055: /**
056: * Creates a new FileRole object.
057: * @param itemManager The item manager.
058: * @param logger The logger.
059: */
060: public FileRole(ItemManager itemManager, Logger logger) {
061: super (itemManager, logger);
062: FileItemManager fileItemManager = (FileItemManager) itemManager;
063: setConfigurationDirectory(fileItemManager
064: .getConfigurationDirectory());
065: }
066:
067: /**
068: * Configure this instance of <code>FileRole</code>
069: * @param config containing the role details
070: * @throws ConfigurationException if the <code>FileRole</code> could not
071: * be configured
072: */
073: public void configure(Configuration config)
074: throws ConfigurationException {
075: new ItemConfiguration().configure(this , config);
076: this .isAssignable = config.getAttributeAsBoolean(
077: ATTR_ASSIGNABLE, true);
078: }
079:
080: /**
081: * Save the role
082: * @throws AccessControlException if the save fails
083: */
084: public void save() throws AccessControlException {
085: DefaultConfigurationSerializer serializer = new DefaultConfigurationSerializer();
086: Configuration config = createConfiguration();
087: File xmlPath = getConfigurationDirectory();
088: File xmlfile = new File(xmlPath, getId()
089: + FileRoleManager.SUFFIX);
090:
091: try {
092: serializer.serializeToFile(xmlfile, config);
093: } catch (Exception e) {
094: throw new AccessControlException(e);
095: }
096: }
097:
098: /**
099: * Create a configuration containing the role details
100: * @return a <code>Configuration</code>
101: */
102: private Configuration createConfiguration() {
103: DefaultConfiguration config = new DefaultConfiguration(ROLE);
104: new ItemConfiguration().save(this , config);
105: return config;
106: }
107:
108: private File configurationDirectory;
109: private boolean isAssignable;
110:
111: /**
112: * Returns the configuration directory.
113: * @return A file object.
114: */
115: public File getConfigurationDirectory() {
116: return this .configurationDirectory;
117: }
118:
119: protected void setConfigurationDirectory(File file) {
120: this .configurationDirectory = file;
121: }
122:
123: public boolean isAssignable() {
124: return this.isAssignable;
125: }
126: }
|