KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > DottedNameGetSetForConfig


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 package com.sun.enterprise.admin.mbeans;
25
26 import com.sun.enterprise.admin.dottedname.*;
27
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.Attribute JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import java.util.Arrays JavaDoc;
32
33 /**
34  * This was originally an inner class within DottedNameGetSetMBeanImpl. For
35  * extensibility reasons this was moved out as a separate class.
36  * @author <a HREF=mailto:lloyd.chambers@sun.com>Lloyd Chambers</a>
37  * @author <a HREF=mailto:shreedhar.ganapathy@sun.com>Shreedhar Ganapathy</a>
38  * Date: Jun 9, 2004
39  * @version $Revision: 1.3 $
40  */

41 /*
42     Implementing subclass for Config dotted names
43  */

44 public class DottedNameGetSetForConfig extends DottedNameGetSetMBeanBase
45   {
46     final DottedNameResolverForAliases mResolver;
47     final DottedNameQuery mQuery;
48
49
50     public DottedNameGetSetForConfig(
51         final MBeanServerConnection JavaDoc conn,
52         final DottedNameRegistry registry,
53         final DottedNameServerInfo serverInfo )
54     {
55         super( conn, registry, serverInfo );
56
57         // DottedNameResolver for regular dotted names needs to account for aliases.
58
mResolver = new DottedNameResolverForAliases( registry, serverInfo );
59
60         mQuery = new DottedNameAliasedQuery( registry, serverInfo );
61     }
62
63
64
65     protected DottedNameResolver
66     getResolver( )
67     {
68         return( mResolver );
69     }
70
71     public Object JavaDoc [] dottedNameSet( final String JavaDoc [] nameValuePairs )
72     {
73         final int numItems = nameValuePairs.length;
74
75         // make a new array of sorted input pairs
76
// do this first, because resulting output may contain a mix of type--
77
// Attribute or Exception making it hard to sort properly.
78
final String JavaDoc [] sortedPairs = new String JavaDoc [ numItems ];
79         for( int i = 0; i < numItems; ++i )
80         {
81             sortedPairs[ i ] = nameValuePairs[ i ];
82         }
83         Arrays.sort( sortedPairs );
84
85
86         final Object JavaDoc [] results = new Object JavaDoc [ sortedPairs.length ];
87         for( int i = 0; i < numItems; ++i )
88         {
89             results[ i ] = dottedNameSet( sortedPairs[ i ] );
90         }
91
92         return( results );
93     }
94
95     /*
96         Return either an Attribute or a Throwable to indicate status.
97      */

98     protected Object JavaDoc dottedNameSet( final String JavaDoc nameValuePair )
99     {
100         Object JavaDoc result = null;
101
102         try
103         {
104             result = doSet( nameValuePair );
105         }
106         catch( Exception JavaDoc e )
107         {
108             // the result will be the exception itself
109
logException( e );
110             result = e;
111         }
112
113         assert( result != null );
114         return( result );
115     }
116
117      public Attribute JavaDoc
118     doSet( String JavaDoc pair )
119         throws Exception JavaDoc
120     {
121         final int delimIndex = pair.indexOf( ASSIGNMENT_DELIM );
122         final boolean delete = delimIndex < 0;
123
124         // if there is no value delimiter ('='), this means to delete the property
125
// this is only supported for properties however
126
final String JavaDoc dottedNameString = delete ? pair : pair.substring( 0, delimIndex );
127         final String JavaDoc valueString = delete ? null : pair.substring( delimIndex + 1, pair.length() );
128
129         final Attribute JavaDoc attr = doSet( dottedNameString, valueString );
130
131         return( attr );
132     }
133
134
135     public Attribute JavaDoc
136     doSet( String JavaDoc dottedNameString, String JavaDoc value )
137         throws Exception JavaDoc
138     {
139         // NOTE: this name includes the value-name
140
final DottedName dn = getDottedName( dottedNameString );
141
142         if ( dn.isWildcardName() )
143         {
144             final String JavaDoc msg = DottedNameStrings.getString(
145                     DottedNameStrings.WILDCARD_DISALLOWED_FOR_SET_KEY,
146                     dottedNameString );
147
148             throw new IllegalArgumentException JavaDoc( msg );
149         }
150
151         ObjectName JavaDoc target = null;
152         final DottedNameForValue dnv = new DottedNameForValue( dn );
153         if ( isDottedNameForServerName( dnv.getPrefix(), dnv.getValueName() ) )
154         {
155             // YUCK special case "server.name" or otherwise the attribute will
156
// be set on the config
157
target = getRegistry().dottedNameToObjectName( dnv.getPrefix().toString() );
158         }
159         else
160         {
161             target = getTarget( dnv, getResolver( ) );
162         }
163
164         final Attribute JavaDoc inAttr = new Attribute JavaDoc( dnv.getValueName(), value );
165
166         Attribute JavaDoc resultAttr = mValueAccessor.setValue( target, inAttr );
167
168         // special meaning of result with null value is that it has been deleted (yuck)
169
// in this case, it is not added to the output list
170
if ( resultAttr != null && resultAttr.getValue() != null )
171         {
172             final String JavaDoc fullName = dnv.getPrefix() + "." + inAttr.getName();
173
174             resultAttr = new Attribute JavaDoc( fullName, resultAttr.getValue() );
175         }
176
177         return( resultAttr );
178     }
179
180
181         protected DottedNameQuery
182     createQuery( )
183     {
184         return( mQuery );
185     }
186
187
188         boolean
189     isServerName( String JavaDoc name )
190         throws DottedNameServerInfo.UnavailableException
191     {
192         boolean isServerName = false;
193
194         isServerName = mServerInfo.getServerNames().contains( name );
195
196         return( isServerName );
197     }
198
199         protected boolean
200     isDottedNameForServerName( final DottedName prefix, final String JavaDoc valueName )
201         throws DottedNameServerInfo.UnavailableException
202     {
203         return( valueName.equals( "name" ) &&
204             isServerName( prefix.getScope() ) && // these tests make sure it's just "<server>.name"
205
prefix.getParts().size() == 0 );
206     }
207
208     /*
209         Create an Attribute for a given prefix, value name and associated value.
210
211         We override for special cases that aliasing doesn't work correctly;
212         namely when names alias into config.
213
214         Aliasing can't work properly in this case because aliasing works on prefixes;
215         the attributes are determine only after an alias has been resolved.
216      */

217     protected Attribute JavaDoc
218     formAttribute( final DottedName prefix, final String JavaDoc valueName, Object JavaDoc value )
219     {
220         // default behavior
221
Attribute JavaDoc attr = super.formAttribute( prefix, valueName, value );
222
223         try
224         {
225             // Is it a special case for server.xxx which is into 'server' itself?
226
if ( isDottedNameForServerName( prefix, valueName ) )
227             {
228                 // <server>.name value has been set to <config>.name value
229
// skip the aliasing and get the name from the server object
230
final ObjectName JavaDoc objectName = getRegistry().dottedNameToObjectName( prefix.getScope() );
231
232                 final Attribute JavaDoc newAttr = mValueAccessor.getValue( objectName, valueName );
233                 assert( newAttr != null );
234                 attr = super.formAttribute( prefix, valueName, newAttr.getValue() );
235             }
236         }
237         catch( Exception JavaDoc e)
238         {
239             logException( e );
240         }
241
242         return( attr );
243     }
244 }
245
Popular Tags