KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > GetResourceInputStreamAction


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

21
22 package org.apache.derby.client.am;
23
24 /**
25  * Java 2 PrivilegedAction encapsulation of attempting to acquire driver-general properties as a System resource.
26  */

27 public class GetResourceInputStreamAction implements java.security.PrivilegedAction JavaDoc {
28     // Name for loading the resource.
29
private String JavaDoc resourceName_ = null;
30     // Path of the resource being loaded.
31
private String JavaDoc resourcePath_ = null;
32     // Class loader being used to load the resource.
33
private String JavaDoc resourceLoaderId_ = null;
34
35     //-------------------- Constructors --------------------
36

37     public GetResourceInputStreamAction(String JavaDoc resourceName) {
38         resourceName_ = resourceName;
39     }
40
41     //-------------------- methods --------------------
42

43     public Object JavaDoc run() {
44         try {
45             ClassLoader JavaDoc contextLoader = Thread.currentThread().getContextClassLoader();
46             if (contextLoader != null) {
47                 java.net.URL JavaDoc resourceUrl = contextLoader.getResource(resourceName_);
48                 if (resourceUrl != null) {
49                     resourcePath_ = resourceUrl.getPath();
50                     resourceLoaderId_ = "Context ClassLoader: " + contextLoader;
51                     return contextLoader.getResourceAsStream(resourceName_);
52                 }
53             }
54             ClassLoader JavaDoc thisLoader = getClass().getClassLoader();
55             if (thisLoader != contextLoader) {
56                 java.net.URL JavaDoc resourceUrl = thisLoader.getResource(resourceName_);
57                 if (resourceUrl != null) {
58                     resourcePath_ = resourceUrl.getPath();
59                     resourceLoaderId_ = "Driver ClassLoader: " + thisLoader;
60                     return thisLoader.getResourceAsStream(resourceName_);
61                 }
62             }
63             ClassLoader JavaDoc systemLoader = ClassLoader.getSystemClassLoader();
64             if (systemLoader != contextLoader &&
65                     systemLoader != thisLoader) {
66                 java.net.URL JavaDoc resourceUrl = systemLoader.getResource(resourceName_);
67                 if (resourceUrl != null) {
68                     resourcePath_ = resourceUrl.getPath();
69                     resourceLoaderId_ = "System ClassLoader: " + systemLoader;
70                     return systemLoader.getResourceAsStream(resourceName_);
71                 }
72             }
73             return null;
74         } catch (java.security.AccessControlException JavaDoc ace) {
75             // This happens in an Applet environment,
76
// so return with null.
77
return null;
78         }
79     }
80
81     public void setResourceName(String JavaDoc resourceName) {
82         resourceName_ = resourceName;
83     }
84
85     public String JavaDoc getResourcePath() {
86         return resourcePath_;
87     }
88
89     public String JavaDoc getResourceLoaderId() {
90         return resourceLoaderId_;
91     }
92
93 }
94
Popular Tags