KickJava   Java API By Example, From Geeks To Geeks.

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


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 /* $Id: FileGroup.java 43237 2004-08-16 15:59:51Z andreas $ */
19
20 package org.apache.lenya.ac.file;
21
22 import java.io.File JavaDoc;
23
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.configuration.DefaultConfiguration;
27 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
28 import org.apache.lenya.ac.AccessControlException;
29 import org.apache.lenya.ac.Item;
30 import org.apache.lenya.ac.impl.AbstractGroup;
31 import org.apache.lenya.ac.impl.ItemConfiguration;
32
33 /**
34  * File-based group implementation.
35  */

36 public class FileGroup extends AbstractGroup implements Item {
37     
38     /**
39      * @see org.apache.lenya.ac.Group#delete()
40      */

41     public void delete() throws AccessControlException {
42         super.delete();
43         getFile().delete();
44     }
45
46     /**
47      * Creates a new FileGroup object.
48      */

49     public FileGroup() {
50     }
51
52     /**
53      * Create a new instance of <code>FileGroup</code>
54      * @param configurationDirectory to which the group will be attached to
55      * @param id the ID of the group
56          */

57     public FileGroup(File JavaDoc configurationDirectory, String JavaDoc id) {
58         super(id);
59         setConfigurationDirectory(configurationDirectory);
60     }
61
62     /**
63      * Configures this file group.
64      * @param config The configuration.
65      * @throws ConfigurationException when something went wrong.
66      */

67     public void configure(Configuration config) throws ConfigurationException {
68         new ItemConfiguration().configure(this, config);
69     }
70     
71     /**
72      * Returns the configuration file.
73      * @return A file object.
74      */

75     protected File JavaDoc getFile() {
76         File JavaDoc xmlPath = getConfigurationDirectory();
77         File JavaDoc xmlFile = new File JavaDoc(xmlPath, getId() + FileGroupManager.SUFFIX);
78         return xmlFile;
79     }
80
81     /**
82      * Save this group
83      *
84      * @throws AccessControlException if the save failed
85      */

86     public void save() throws AccessControlException {
87         DefaultConfigurationSerializer serializer = new DefaultConfigurationSerializer();
88         Configuration config = createConfiguration();
89         File JavaDoc xmlfile = getFile();
90
91         try {
92             serializer.serializeToFile(xmlfile, config);
93         } catch (Exception JavaDoc e) {
94             throw new AccessControlException(e);
95         }
96     }
97
98     /**
99      * Group configuration element.
100      */

101     public static final String JavaDoc GROUP = "group";
102
103     /**
104      * Create a configuration containing the group details
105      *
106      * @return a <code>Configuration</code>
107      */

108     private Configuration createConfiguration() {
109         DefaultConfiguration config = new DefaultConfiguration(GROUP);
110         new ItemConfiguration().save(this, config);
111
112         return config;
113     }
114
115     private File JavaDoc configurationDirectory;
116
117     /**
118      * Returns the configuration directory.
119      * @return A file object.
120      */

121     protected File JavaDoc getConfigurationDirectory() {
122         return configurationDirectory;
123     }
124
125     /**
126      * @see org.apache.lenya.ac.Item#setConfigurationDirectory(java.io.File)
127      */

128     public void setConfigurationDirectory(File JavaDoc configurationDirectory) {
129         assert (configurationDirectory != null) && configurationDirectory.isDirectory();
130         this.configurationDirectory = configurationDirectory;
131     }
132 }
133
Popular Tags