KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > repository > Repository


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.repository;
24
25 import java.util.*;
26 import javax.naming.*;
27 // IASRI 4660742 START
28
import java.util.logging.*;
29 import com.sun.logging.*;
30 // IASRI 4660742 END
31

32 /*
33  * @author Harish Prabandham
34  */

35 class Repository {
36
37 // IASRI 4660742 START
38
private static Logger _logger=null;
39     static{
40        _logger=LogDomains.getLogger(LogDomains.ROOT_LOGGER);
41         }
42 // IASRI 4660742 END
43
/**
44      * Finds a value corresponding to a name stored within this
45      * Repository.
46      * @return A string Value corresponding to the name within the
47      * repository or null if no such value exists.
48      */

49     public String JavaDoc find(String JavaDoc name) {
50         try {
51             String JavaDoc value = (String JavaDoc) ctx.lookup(name);
52             // System.out.println(name + " is bound to " + value);
53
// START OF IASRI 4660742
54
// _logger.log(Level.FINE,name + " is bound to " + value);
55
// END OF IASRI 4660742
56
return value;
57         } catch (Exception JavaDoc e) {
58       // e.printStackTrace();
59
// START OF IASRI 4660742
60
// _logger.log(Level.SEVERE,"enterprise.findinrepository_exception",e);
61
// END OF IASRI 4660742
62
return null;
63         }
64     }
65     
66     /**
67      * Adds a value corresponding to a name within this Repository.
68      * @return A value true indicates that the value was successfully
69      * added to the repository.
70      */

71     public boolean add(String JavaDoc name, String JavaDoc value) {
72         try {
73             ctx.rebind(name, value);
74             // System.out.println("Added " + name + ", " + value);
75
// START OF IASRI 4660742
76
//if(_logger.isLoggable(Level.FINE))
77
// _logger.log(Level.FINE,"Added " + name + ", " + value);
78
// END OF IASRI 4660742
79
return true;
80     } catch (NamingException ne) {
81 // IASRI 4660742 ne.printStackTrace();
82
// START OF IASRI 4660742
83
_logger.log(Level.SEVERE,"enterprise.addinrepository_exception",ne);
84 // END OF IASRI 4660742
85
return false;
86     }
87     }
88
89     public boolean remove(String JavaDoc name){
90     try {
91         ctx.unbind(name);
92         // System.out.println("Added " + name + ", " + value);
93
// START OF IASRI 4660742
94
//if(_logger.isLoggable(Level.FINE))
95
// _logger.log(Level.FINE,"Added " + name + ", " + value);
96
// END OF IASRI 4660742
97
return true;
98     } catch (NamingException ne) {
99 // IASRI 4660742 ne.printStackTrace();
100
// START OF IASRI 4660742
101
_logger.log(Level.SEVERE,"enterprise.delinrepository_exception",ne);
102 // END OF IASRI 4660742
103
return false;
104     }
105     }
106
107     public String JavaDoc[] keys()
108     {
109         Vector v = new Vector(10);
110         Enumeration e = null;
111         try {
112             e = ctx.listBindings("");
113         } catch (NamingException ne) {
114 // IASRI 4660742 ne.printStackTrace();
115
// START OF IASRI 4660742
116
_logger.log(Level.SEVERE,"enterprise.naming_exception",ne);
117 // END OF IASRI 4660742
118
}
119
120         while((e != null) && e.hasMoreElements())
121         {
122             Binding b = (Binding) e.nextElement();
123             v.add(b.getName());
124         }
125
126         String JavaDoc[] keynames = new String JavaDoc[v.size()];
127         v.copyInto(keynames);
128
129         return keynames;
130     }
131
132     public String JavaDoc getName()
133     {
134         return RepositoryContext.getRepositoryName(name);
135     }
136     
137     /**
138      * Constructor.....
139      */

140     public Repository(String JavaDoc repositoryName) {
141         Properties env = new Properties();
142         env.put("java.naming.factory.initial",
143                 "com.sun.enterprise.repository.RepositoryInitContextFactory");
144         env.put("com.sun.enterprise.repository.name", repositoryName);
145     name = repositoryName;
146     init(env);
147     }
148
149
150     /**
151      * Constructor.....
152      */

153     public Repository(String JavaDoc repositoryName, String JavaDoc repositoryDir) {
154         Properties env = new Properties();
155         env.put("java.naming.factory.initial",
156                 "com.sun.enterprise.repository.RepositoryInitContextFactory");
157         env.put("com.sun.enterprise.repository.name", repositoryName);
158         env.put("com.sun.enterprise.repository.dir", repositoryDir);
159     name = repositoryName;
160     init(env);
161     }
162
163
164     /**
165      * Initializing the context...
166      */

167     private void init(Properties env) {
168         try {
169             ctx = new InitialContext(env);
170         } catch (NamingException ne) {
171 // IASRI 4660742 ne.printStackTrace();
172
// START OF IASRI 4660742
173
_logger.log(Level.SEVERE,"enterprise.naming_exception",ne);
174 // END OF IASRI 4660742
175
ctx = null;
176         }
177     }
178
179     
180     private Context ctx = null;
181     private String JavaDoc name = null;
182 }
183
Popular Tags