KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > file > FileRole


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.lenya.ac.file;
19
20 import java.io.File JavaDoc;
21
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.configuration.DefaultConfiguration;
25 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
26 import org.apache.lenya.ac.AccessControlException;
27 import org.apache.lenya.ac.Item;
28 import org.apache.lenya.ac.impl.AbstractRole;
29 import org.apache.lenya.ac.impl.ItemConfiguration;
30
31 /**
32  * File-based role implementation.
33  * @version $Id: FileRole.java 43237 2004-08-16 15:59:51Z andreas $
34  */

35 public class FileRole extends AbstractRole implements Item {
36     
37     /**
38     * Creates a new file role.
39     * @param configurationDirectory The configuration directory.
40     * @param id The role ID.
41     */

42     public FileRole(File JavaDoc configurationDirectory, String JavaDoc id) {
43         setId(id);
44         setConfigurationDirectory(configurationDirectory);
45     }
46
47     public static final String JavaDoc ROLE = "role";
48
49     /**
50      * Creates a new FileRole object.
51      */

52     public FileRole() {
53     }
54
55     /**
56      * Configure this instance of <code>FileRole</code>
57      *
58      * @param config containing the role details
59      * @throws ConfigurationException if the <code>FileRole</code> could not be configured
60      */

61     public void configure(Configuration config) throws ConfigurationException {
62         new ItemConfiguration().configure(this, config);
63     }
64
65     /**
66      * Save the role
67      *
68      * @throws AccessControlException if the save fails
69      */

70     public void save() throws AccessControlException {
71         DefaultConfigurationSerializer serializer = new DefaultConfigurationSerializer();
72         Configuration config = createConfiguration();
73         File JavaDoc xmlPath = getConfigurationDirectory();
74         File JavaDoc xmlfile = new File JavaDoc(xmlPath, getId() + FileRoleManager.SUFFIX);
75
76         try {
77             serializer.serializeToFile(xmlfile, config);
78         } catch (Exception JavaDoc e) {
79             throw new AccessControlException(e);
80         }
81     }
82
83     /**
84      * Create a configuration containing the role details
85      *
86      * @return a <code>Configuration</code>
87      */

88     private Configuration createConfiguration() {
89         DefaultConfiguration config = new DefaultConfiguration(ROLE);
90         new ItemConfiguration().save(this, config);
91         return config;
92     }
93
94     private File JavaDoc configurationDirectory;
95
96     /**
97      * Returns the configuration directory.
98      * @return A file object.
99      */

100     public File JavaDoc getConfigurationDirectory() {
101         return configurationDirectory;
102     }
103
104     /**
105      * @see org.apache.lenya.ac.Item#setConfigurationDirectory(java.io.File)
106      */

107     public void setConfigurationDirectory(File JavaDoc file) {
108         configurationDirectory = file;
109     }
110 }
111
Popular Tags