KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > install > DaisyWikiInit


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package org.outerj.daisy.install;
17
18 import java.io.FileInputStream JavaDoc;
19 import java.util.Properties JavaDoc;
20
21 import org.apache.commons.cli.CommandLine;
22 import org.apache.commons.cli.CommandLineParser;
23 import org.apache.commons.cli.HelpFormatter;
24 import org.apache.commons.cli.Option;
25 import org.apache.commons.cli.Options;
26 import org.apache.commons.cli.ParseException;
27 import org.apache.commons.cli.PosixParser;
28 import org.outerj.daisy.repository.Credentials;
29 import org.outerj.daisy.repository.Repository;
30 import org.outerj.daisy.repository.RepositoryManager;
31 import org.outerj.daisy.repository.acl.AccessManager;
32 import org.outerj.daisy.repository.acl.Acl;
33 import org.outerj.daisy.repository.acl.AclActionType;
34 import org.outerj.daisy.repository.acl.AclEntry;
35 import org.outerj.daisy.repository.acl.AclObject;
36 import org.outerj.daisy.repository.acl.AclPermission;
37 import org.outerj.daisy.repository.acl.AclSubjectType;
38 import org.outerj.daisy.repository.clientimpl.RemoteRepositoryManager;
39 import org.outerj.daisy.repository.user.Role;
40 import org.outerj.daisy.repository.user.RoleNotFoundException;
41 import org.outerj.daisy.repository.user.User;
42 import org.outerj.daisy.repository.user.UserManager;
43 import org.outerj.daisy.repository.user.UserNotFoundException;
44 import org.outerj.daisy.util.VersionHelper;
45
46 public class DaisyWikiInit {
47     private long guestRoleId;
48
49     private Repository repository;
50
51     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
52         new DaisyWikiInit().install(args);
53     }
54
55     private void install(String JavaDoc[] args) throws Exception JavaDoc {
56         Options options = new Options();
57         options.addOption(new Option("c", "conf", true, "Configuration file for automated install"));
58         options.addOption(new Option("v", "version", false, "Print version info"));
59
60         CommandLineParser parser = new PosixParser();
61         CommandLine cmd = null;
62         try {
63             cmd = parser.parse(options, args);
64         } catch (ParseException e) {
65             HelpFormatter help = new HelpFormatter();
66             help.printHelp("daisy-wiki-init", options, true);
67             System.exit(0);
68         }
69
70         if (cmd.hasOption('v')) {
71             System.out.println(VersionHelper.getVersionString(getClass().getClassLoader(), "org/outerj/daisy/install/versioninfo.properties"));
72             System.exit(0);
73         }
74         
75         if (cmd.hasOption("c")) {
76             FileInputStream JavaDoc is = null;
77             try {
78                 is = new FileInputStream JavaDoc(cmd.getOptionValue("c"));
79                 Properties JavaDoc props = new Properties JavaDoc();
80                 props.load(is);
81
82                 Credentials credentials = new Credentials(InstallHelper.getPropertyValue(props, InitialisationProperties.DAISY_LOGIN), InstallHelper
83                         .getPropertyValue(props, InitialisationProperties.DAISY_PASSWORD));
84
85                 RepositoryManager repositoryManager = new RemoteRepositoryManager(InstallHelper.getPropertyValue(props, InitialisationProperties.DAISY_URL),
86                         credentials);
87                 repository = repositoryManager.getRepository(credentials);
88                 repository.switchRole(Role.ADMINISTRATOR);
89             } finally {
90                 if (is != null)
91                     is.close();
92                 else
93                     System.out.println("Could not open " + cmd.getOptionValue("c") + " exiting ...");
94             }
95
96         } else {
97             InstallHelper.printTitle("Daisy Wiki Initialisation");
98             System.out.println();
99             init();
100         }
101
102         installGuestUser();
103         installACL();
104         installSchema();
105
106         System.out.println();
107         System.out.println("Finished.");
108     }
109
110     private void init() throws Exception JavaDoc {
111         repository = InstallHelper.promptRepository();
112     }
113
114     private void installGuestUser() throws Exception JavaDoc {
115         InstallHelper.printTitle("Creating guest user and role.");
116
117         UserManager userManager = repository.getUserManager();
118         Role role;
119         try {
120             role = userManager.getRole("guest", false);
121             System.out.println("Existing guest role found, id = " + role.getId());
122         } catch (RoleNotFoundException e) {
123             role = userManager.createRole("guest");
124             role.save();
125             System.out.println("Guest role created, id = " + role.getId());
126         }
127         this.guestRoleId = role.getId();
128
129         User user;
130         try {
131             user = userManager.getUser("guest", false);
132             System.out.println("Existing guest user found, id = " + user.getId());
133         } catch (UserNotFoundException e) {
134             user = userManager.createUser("guest");
135             user.setPassword("guest");
136             user.addToRole(role);
137             user.save();
138             System.out.println("Guest user created, id = " + user.getId());
139         }
140     }
141
142     private void installSchema() throws Exception JavaDoc {
143         System.out.println();
144         InstallHelper.printTitle("Schema creation");
145         System.out.println();
146         SchemaUploader schemaUploader = new SchemaUploader(getClass().getClassLoader().getResourceAsStream("org/outerj/daisy/install/daisywiki_schema.xml"));
147         schemaUploader.load(repository.getRepositorySchema());
148     }
149
150     private void installACL() throws Exception JavaDoc {
151         System.out.println();
152         InstallHelper.printTitle("ACL initialisation");
153         System.out.println();
154
155         AccessManager accessManager = repository.getAccessManager();
156         Acl acl = accessManager.getStagingAcl();
157         if (acl.size() > 0) {
158             System.out.println("ACL is not empty -- will not touch it.");
159             System.out.println("");
160             return;
161         }
162
163         System.out.println("A default ACL will be installed. This will limit the users with role 'guest'");
164         System.out.println("to read operations. All other users will have both read and write privileges.");
165
166         acl.clear();
167         AclObject aclObject = acl.createNewObject("true");
168         acl.add(aclObject);
169
170         AclEntry aclEntry = aclObject.createNewEntry(AclSubjectType.EVERYONE, -1);
171         aclEntry.set(AclPermission.READ_LIVE, AclActionType.GRANT);
172         aclEntry.set(AclPermission.READ, AclActionType.GRANT);
173         aclEntry.set(AclPermission.WRITE, AclActionType.GRANT);
174         aclEntry.set(AclPermission.PUBLISH, AclActionType.GRANT);
175         aclEntry.set(AclPermission.DELETE, AclActionType.GRANT);
176         aclObject.add(aclEntry);
177
178         aclEntry = aclObject.createNewEntry(AclSubjectType.ROLE, guestRoleId);
179         aclEntry.set(AclPermission.READ_LIVE, AclActionType.GRANT);
180         aclEntry.set(AclPermission.READ, AclActionType.GRANT);
181         aclEntry.set(AclPermission.WRITE, AclActionType.DENY);
182         aclEntry.set(AclPermission.PUBLISH, AclActionType.DENY);
183         aclEntry.set(AclPermission.DELETE, AclActionType.DENY);
184         aclObject.add(aclEntry);
185
186         acl.save();
187         accessManager.copyStagingToLive();
188
189         System.out.println("ACL configured.");
190     }
191
192     private class InitialisationProperties {
193         public static final String JavaDoc DAISY_URL = "daisyUrl";
194
195         public static final String JavaDoc DAISY_LOGIN = "daisyLogin";
196
197         public static final String JavaDoc DAISY_PASSWORD = "daisyPassword";
198     }
199 }
200
Popular Tags