KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > component > ComponentResourceHandler


1 /*
2  * $Id: ComponentResourceHandler.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.component;
26
27 import java.io.InputStream JavaDoc;
28 import java.net.URL JavaDoc;
29
30 import org.ofbiz.base.config.GenericConfigException;
31 import org.ofbiz.base.config.ResourceHandler;
32 import org.ofbiz.base.util.UtilXml;
33 import org.ofbiz.base.util.Debug;
34
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37
38 /**
39  * Contains resource information and provides for loading data
40  *
41  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
42  * @version $Rev: 5462 $
43  * @since 3.0
44  */

45 public class ComponentResourceHandler implements ResourceHandler {
46
47     public static final String JavaDoc module = ComponentResourceHandler.class.getName();
48     protected String JavaDoc componentName;
49     protected String JavaDoc loaderName;
50     protected String JavaDoc location;
51
52     public ComponentResourceHandler(String JavaDoc componentName, Element JavaDoc element) {
53         this.componentName = componentName;
54         this.loaderName = element.getAttribute("loader");
55         this.location = element.getAttribute("location");
56     }
57
58     public ComponentResourceHandler(String JavaDoc componentName, String JavaDoc loaderName, String JavaDoc location) {
59         this.componentName = componentName;
60         this.loaderName = loaderName;
61         this.location = location;
62         if (Debug.verboseOn()) Debug.logVerbose("Created " + this.toString(), module);
63     }
64
65     public String JavaDoc getLoaderName() {
66         return this.loaderName;
67     }
68
69     public String JavaDoc getLocation() {
70         return this.location;
71     }
72
73     public Document JavaDoc getDocument() throws GenericConfigException {
74         try {
75             return UtilXml.readXmlDocument(this.getStream(), this.getFullLocation());
76         } catch (org.xml.sax.SAXException JavaDoc e) {
77             throw new GenericConfigException("Error reading " + this.toString(), e);
78         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
79             throw new GenericConfigException("Error reading " + this.toString(), e);
80         } catch (java.io.IOException JavaDoc e) {
81             throw new GenericConfigException("Error reading " + this.toString(), e);
82         }
83     }
84
85     public InputStream JavaDoc getStream() throws GenericConfigException {
86         return ComponentConfig.getStream(componentName, loaderName, location);
87     }
88
89     public URL JavaDoc getURL() throws GenericConfigException {
90         return ComponentConfig.getURL(componentName, loaderName, location);
91     }
92
93     public boolean isFileResource() throws GenericConfigException {
94         return ComponentConfig.isFileResourceLoader(componentName, loaderName);
95     }
96
97     public String JavaDoc getFullLocation() throws GenericConfigException {
98         return ComponentConfig.getFullLocation(componentName, loaderName, location);
99     }
100
101     public boolean equals(Object JavaDoc obj) {
102         if (obj instanceof ComponentResourceHandler) {
103             ComponentResourceHandler other = (ComponentResourceHandler) obj;
104
105             if (this.loaderName.equals(other.loaderName) &&
106                 this.componentName.equals(other.componentName) &&
107                 this.location.equals(other.location)) {
108                 return true;
109             }
110         }
111         return false;
112     }
113
114     public int hashCode() {
115         // the hashCode will weight by a combination componentName and the combination of loaderName and location
116
return (this.componentName.hashCode() + ((this.loaderName.hashCode() + this.location.hashCode()) >> 1)) >> 1;
117     }
118
119     public String JavaDoc toString() {
120         return "ComponentResourceHandler from XML file [" + this.componentName + "] with loaderName [" + loaderName + "] and location [" + location + "]";
121     }
122 }
123
Popular Tags