KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > descriptor > xml > PathHandler


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.descriptor.xml;
32
33 import org.objectweb.proactive.core.xml.handler.BasicUnmarshaller;
34 import org.objectweb.proactive.core.xml.io.Attributes;
35
36
37 /**
38  * This class receives deployment events
39  *
40  * @author Lionel Mestre
41  * @version 1.0
42  */

43 public class PathHandler extends BasicUnmarshaller implements ProActiveDescriptorConstants {
44     //
45
// ----- PRIVATE MEMBERS -----------------------------------------------------------------------------------
46
//
47

48     private static final String JavaDoc ORIGIN_ATTRIBUTE = "origin";
49     private static final String JavaDoc USER_HOME_ORIGIN = "user.home";
50     private static final String JavaDoc WORKING_DIRECTORY_ORIGIN = "user.dir";
51     private static final String JavaDoc FROM_CLASSPATH_ORIGIN = "user.classpath";
52    // private static final String PROACTIVE_ORIGIN = "proactive.home";
53
private static final String JavaDoc DEFAULT_ORIGIN = USER_HOME_ORIGIN;
54     private static final String JavaDoc VALUE_ATTRIBUTE = "value";
55     private static final String JavaDoc proActiveDir = System.getProperty(
56             "proactive.home");
57     private static final String JavaDoc userDir = System.getProperty("user.dir");
58     private static final String JavaDoc userHome = System.getProperty("user.home");
59     private static final String JavaDoc javaHome = System.getProperty("java.home");
60     private static final String JavaDoc pathSeparator = System.getProperty(
61             "path.separator");
62     private static final String JavaDoc fileSeparator = System.getProperty(
63             "file.separator");
64             
65     //static Logger logger = Logger.getLogger(PathHandler.class.getName());
66

67     //
68
// ----- CONSTRUCTORS -----------------------------------------------------------------------------------
69
//
70
public PathHandler() {
71     }
72
73     //
74
// ----- PUBLIC METHODS -----------------------------------------------------------------------------------
75
//
76
public Object JavaDoc getResultObject() throws org.xml.sax.SAXException JavaDoc {
77         return super.getResultObject();
78     }
79
80     public void startContextElement(String JavaDoc name, Attributes attributes)
81         throws org.xml.sax.SAXException JavaDoc {
82         // read from XML
83
// String type = attributes.getValue(TYPE_ATTRIBUTE);
84
// if (! checkNonEmpty(type)) type = DEFAULT_TYPE;
85
String JavaDoc origin = attributes.getValue(ORIGIN_ATTRIBUTE);
86         if (!checkNonEmpty(origin)) {
87             origin = DEFAULT_ORIGIN;
88         }
89         String JavaDoc value = attributes.getValue(VALUE_ATTRIBUTE);
90 // if (logger.isDebugEnabled()) {
91
// logger.debug("Found Path Element origin=" + origin + " value=" +
92
// value);
93
// }
94
if (!checkNonEmpty(value)) {
95             throw new org.xml.sax.SAXException JavaDoc(
96                 "Path element defined without a value");
97         }
98
99         // build the associated string
100
if (name.equals(ABS_PATH_TAG)) {
101             setResultObject(value);
102         } else if (name.equals(REL_PATH_TAG)) {
103             if (origin.equals(USER_HOME_ORIGIN)) {
104                 setResultObject(resolvePath(userHome, value));
105             } else if (origin.equals(WORKING_DIRECTORY_ORIGIN)) {
106                 setResultObject(resolvePath(userDir, value));
107 // } else if (origin.equals(PROACTIVE_ORIGIN)) {
108
// setResultObject(resolvePath(proActiveDir, value));
109
} else if (origin.equals(FROM_CLASSPATH_ORIGIN)) {
110                 setResultObject(resolvePathFromClasspath(value));
111             } else {
112                 throw new org.xml.sax.SAXException JavaDoc(
113                     "Relative Path element defined with an unknown origin=" +
114                     origin);
115             }
116         }
117     }
118
119     //
120
// ----- PRIVATE METHODS -----------------------------------------------------------------------------------
121
//
122
private String JavaDoc resolvePath(String JavaDoc origin, String JavaDoc value) {
123         java.io.File JavaDoc originDirectory = new java.io.File JavaDoc(origin);
124         // in case of relative path, if the user put a / then remove it transparently
125
if(value.startsWith("/")){
126             value = value.substring(1);
127         }
128         java.io.File JavaDoc file = new java.io.File JavaDoc(originDirectory, value);
129         return file.getAbsolutePath();
130     }
131
132     private String JavaDoc resolvePathFromClasspath(String JavaDoc value) {
133         ClassLoader JavaDoc cl = this.getClass().getClassLoader();
134         java.net.URL JavaDoc url = cl.getResource(value);
135         return url.getPath();
136     }
137
138     //
139
// ----- INNER CLASSES -----------------------------------------------------------------------------------
140
//
141
}
142
Popular Tags