KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > support > AliasMgrHashMapImpl


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/support/AliasMgrHashMapImpl.java,v 1.3 2005/12/25 03:45:43 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:43 $
28  */

29  
30
31 package com.sun.cli.jmx.support;
32
33 import java.util.Set JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.FileWriter JavaDoc;
39 import java.io.FileReader JavaDoc;
40
41 public final class AliasMgrHashMapImpl implements AliasMgrSPI
42 {
43     final HashMap JavaDoc mMap;
44     String JavaDoc mFilename;
45     
46     public final static String JavaDoc DEFAULT_FILENAME = "aliases.txt";
47     
48         public
49     AliasMgrHashMapImpl()
50     {
51         mMap = new HashMap JavaDoc();
52     }
53     
54         public void
55     create( String JavaDoc aliasName, String JavaDoc value ) throws Exception JavaDoc
56     {
57         mMap.put( aliasName, value );
58         
59         save();
60     }
61     
62         public String JavaDoc
63     get( String JavaDoc aliasName )
64     {
65         return( (String JavaDoc)mMap.get( aliasName ) );
66     }
67     
68         public void
69     delete( String JavaDoc aliasName )
70         throws Exception JavaDoc
71     {
72         mMap.remove( aliasName );
73         save();
74     }
75     
76         public Set JavaDoc
77     getNames()
78     {
79         return( mMap.keySet() );
80     }
81     
82         public void
83     save( )
84         throws IOException JavaDoc
85     {
86         if ( mFilename != null )
87         {
88             save( new File JavaDoc( mFilename ) );
89         }
90     }
91     
92         public void
93     save( File JavaDoc theFile )
94         throws IOException JavaDoc
95     {
96         mFilename = theFile.getPath();
97         
98         final Set JavaDoc names = getNames();
99         final Iterator JavaDoc iter = names.iterator();
100         final String JavaDoc [] pairs = new String JavaDoc [ names.size() ];
101         
102         for( int i = 0; i < pairs.length; ++i )
103         {
104             final String JavaDoc name = (String JavaDoc)iter.next();
105
106             pairs[ i ] = name + "=" + get( name );
107         }
108         
109         final FileWriter JavaDoc out = new FileWriter JavaDoc( theFile );
110         
111         for( int i = 0; i < pairs.length; ++i )
112         {
113             out.write( pairs[ i ] + "\n" );
114         }
115         
116         out.close();
117     }
118     
119         private String JavaDoc
120     readLine( FileReader JavaDoc in )
121         throws IOException JavaDoc
122     {
123         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
124         
125         while ( true )
126         {
127             final int i =in.read();
128             if ( i < 0 )
129             {
130                 return( null );
131             }
132         
133             final char theChar = (char)i;
134             if ( theChar == '\n' || theChar == '\r' )
135             {
136                 // ignore blank lines
137
if ( buf.length() == 0 )
138                     continue;
139                 break;
140             }
141             
142             buf.append( theChar );
143         }
144         return( buf.toString() );
145     }
146     
147         public void
148     load( File JavaDoc theFile )
149         throws Exception JavaDoc
150     {
151         mFilename = theFile.getPath();
152         
153         final FileReader JavaDoc in = new FileReader JavaDoc( theFile );
154         
155         while ( true )
156         {
157             final String JavaDoc pair = readLine( in );
158             if ( pair == null )
159                 break;
160                 
161             final int separatorOffset = pair.indexOf( '=' );
162             
163             final String JavaDoc name = pair.substring( 0, separatorOffset );
164             final String JavaDoc value = pair.substring( separatorOffset + 1, pair.length() );
165             
166             create( name, value );
167         }
168         
169         in.close();
170     }
171 };
172
173
174
Popular Tags