KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > cmd > CmdEnvImpl


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  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/cmd/CmdEnvImpl.java,v 1.3 2005/12/25 03:45:29 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:29 $
28  */

29  
30
31 package com.sun.cli.jmx.cmd;
32
33 import java.util.Properties JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.FileInputStream JavaDoc;
38 import java.io.FileOutputStream JavaDoc;
39 import java.io.FileNotFoundException JavaDoc;
40 import java.io.IOException JavaDoc;
41
42 class ValueData
43 {
44     Object JavaDoc mValue;
45     boolean mAllowPersistence;
46     
47     ValueData( Object JavaDoc value )
48     {
49         mValue = value;
50         mAllowPersistence = false;
51     }
52     
53     ValueData( Object JavaDoc value, boolean allowPersistence )
54     {
55         mValue = value;
56         mAllowPersistence = allowPersistence;
57     }
58 }
59
60 public final class CmdEnvImpl implements CmdEnv
61 {
62     private final java.util.HashMap JavaDoc mEnv;
63     
64     
65         private final static void
66     dm( Object JavaDoc o )
67     {
68         System.out.println( o.toString() );
69     }
70     
71         public
72     CmdEnvImpl()
73     {
74         mEnv = new java.util.HashMap JavaDoc();
75     }
76     
77         public void
78     put( String JavaDoc key, Object JavaDoc value, boolean allowPersistence )
79     {
80         final ValueData data = new ValueData( value, allowPersistence);
81         
82         mEnv.put( key, data );
83     }
84     
85         public Object JavaDoc
86     get( String JavaDoc key )
87     {
88         final ValueData data = (ValueData)mEnv.get( key );
89         final Object JavaDoc value = (data != null) ? data.mValue : null;
90         
91         return( value );
92     }
93     
94         public void
95     remove( String JavaDoc key )
96     {
97         mEnv.remove( key );
98     }
99     
100         public java.util.Set JavaDoc
101     getKeys()
102     {
103         return( mEnv.keySet( ) );
104     }
105         
106         public boolean
107     isPersistable( String JavaDoc key )
108     {
109         final ValueData data = (ValueData)mEnv.get( key );
110         final boolean isPersistable = data != null && data.mAllowPersistence;
111         
112         return( isPersistable );
113     }
114     
115             Properties JavaDoc
116     exportProperties( )
117     {
118         final Properties JavaDoc props = new Properties JavaDoc();
119         
120         final Iterator JavaDoc iter = getKeys().iterator();
121         while( iter.hasNext() )
122         {
123             final String JavaDoc key = (String JavaDoc)iter.next();
124             
125             if ( isPersistable( key ) )
126             {
127                 props.put( key, get( key ).toString() );
128             }
129         }
130         
131         return( props );
132     }
133     
134         void
135     importProperties( Properties JavaDoc props )
136     {
137         final Enumeration JavaDoc propertyNames = props.propertyNames();
138         
139         while ( propertyNames.hasMoreElements() )
140         {
141             final String JavaDoc name = (String JavaDoc)propertyNames.nextElement();
142             final String JavaDoc value = props.getProperty( name );
143             
144             put( name, value, true );
145         }
146     }
147     
148     private final static String JavaDoc HEADER = "jmxadmin environment properties";
149     
150         void
151     load( File JavaDoc file )
152         throws IOException JavaDoc
153     {
154         FileInputStream JavaDoc inputStream = null;
155         
156         try
157         {
158             inputStream = new FileInputStream JavaDoc( file );
159             
160             final Properties JavaDoc props = new Properties JavaDoc();
161             props.load( inputStream );
162             inputStream.close();
163         
164             importProperties( props );
165         }
166         catch( FileNotFoundException JavaDoc e )
167         {
168             // ignore--OK if it does not exist
169
if ( inputStream != null )
170             {
171                 inputStream.close();
172             }
173         }
174     }
175     
176         void
177     store( File JavaDoc file )
178         throws FileNotFoundException JavaDoc, IOException JavaDoc
179     {
180         final Properties JavaDoc props = exportProperties( );
181         
182         FileOutputStream JavaDoc outputStream = null;
183         
184         try
185         {
186             outputStream = new FileOutputStream JavaDoc( file );
187             props.store( outputStream, HEADER );
188             outputStream.close();
189         }
190         catch( Exception JavaDoc e )
191         {
192             System.err.println( "can't store environment" );
193         }
194         finally
195         {
196             if ( outputStream != null )
197             {
198                 outputStream.close();
199             }
200         }
201     }
202     
203 };
204
205
206
207
208
209     
Popular Tags