KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > Projector


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

20 package org.apache.slide.projector;
21
22 import java.io.InputStream JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import org.apache.commons.httpclient.Credentials;
28 import org.apache.commons.httpclient.UsernamePasswordCredentials;
29 import org.apache.slide.projector.i18n.ErrorMessage;
30 import org.apache.slide.projector.repository.Configurable;
31 import org.apache.slide.projector.repository.Repository;
32 import org.jdom.Document;
33 import org.jdom.Element;
34 import org.jdom.input.SAXBuilder;
35
36 public class Projector {
37     private final static Logger JavaDoc logger = Logger.getLogger(Projector.class.getName());
38
39     private static Credentials credentials;
40     private static Repository repository;
41     private static String JavaDoc projectorDir, applicationsDir, workDir;
42     
43     public static void configure(InputStream JavaDoc configuration) throws ConfigurationException {
44         logger.log(Level.INFO, "Starting projector");
45         SAXBuilder saxBuilder = new SAXBuilder();
46         try {
47             Document document = saxBuilder.build(configuration);
48             Element credentialsElement = document.getRootElement().getChild("credentials");
49             String JavaDoc user = credentialsElement.getAttributeValue("user");
50             String JavaDoc password = credentialsElement.getAttributeValue("password");
51             credentials = new UsernamePasswordCredentials(user, password);
52             Element repositoryElement = document.getRootElement().getChild("repository");
53             String JavaDoc repositoryClassname = repositoryElement.getAttributeValue("class");
54             Class JavaDoc repositoryClass = Class.forName(repositoryClassname);
55             Method JavaDoc getInstanceMethod = repositoryClass.getMethod("getInstance", new Class JavaDoc[0]);
56             repository = (Repository)getInstanceMethod.invoke(null, null);
57             Element configurationElement = repositoryElement.getChild("configuration");
58             if ( repository instanceof Configurable ) {
59                 ((Configurable)repository).configure(configurationElement);
60             }
61             Element pathsElement = repositoryElement.getChild("paths");
62             projectorDir = pathsElement.getChildText("projector");
63             applicationsDir = pathsElement.getChildText("applications");
64             workDir = pathsElement.getChildText("work");
65         } catch (Exception JavaDoc e) {
66             throw new ConfigurationException(new ErrorMessage("projector/configurationFailed"), e);
67         }
68     }
69     
70     public static Repository getRepository() {
71         return repository;
72     }
73     
74     public static String JavaDoc getApplicationsDir() {
75         return applicationsDir;
76     }
77     
78     public static String JavaDoc getProjectorDir() {
79         return projectorDir;
80     }
81     
82     public static String JavaDoc getWorkDir() {
83         return workDir;
84     }
85
86     public static Credentials getCredentials() {
87         return credentials;
88     }
89 }
Popular Tags