KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > config > ClusteredServerConfigFactory


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.management.config;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.AttributeList JavaDoc;
33
34 import com.sun.appserv.management.base.QueryMgr;
35 import com.sun.appserv.management.base.Util;
36 import com.sun.appserv.management.base.XTypes;
37
38 import com.sun.appserv.management.config.ServerConfigKeys;
39 import com.sun.appserv.management.config.ServerRefConfig;
40 import com.sun.appserv.management.config.ServerRefConfigCR;
41 import com.sun.appserv.management.config.ClusteredServerConfig;
42 import com.sun.appserv.management.config.ClusterConfig;
43
44 import com.sun.appserv.management.util.misc.MapUtil;
45 import com.sun.appserv.management.util.misc.CollectionUtil;
46
47
48 import com.sun.enterprise.management.support.oldconfig.OldServersMBean;
49
50 public final class ClusteredServerConfigFactory extends StandaloneServerConfigFactory
51 {
52         public
53     ClusteredServerConfigFactory( final ConfigFactoryCallback callbacks )
54     {
55         super( callbacks );
56     }
57     
58         private boolean
59     getLBEnabled( final ClusterConfig clusterConfig )
60     {
61         final Map JavaDoc<String JavaDoc,ServerRefConfig> refs = clusterConfig.getServerRefConfigMap();
62         
63         boolean enabled = false;
64         // take lb-enabled from the first server
65
if ( refs.keySet().size() != 0 )
66         {
67             final ServerRefConfig first = refs.values().iterator().next();
68             enabled = first.getLBEnabled();
69         }
70         
71         return enabled;
72     }
73     
74         public ObjectName JavaDoc
75     create(
76         final String JavaDoc serverName,
77         final String JavaDoc clusterName,
78         final String JavaDoc nodeAgentName,
79         final Map JavaDoc<String JavaDoc,String JavaDoc> optional)
80     {
81         final ClusterConfig clusterConfig =
82             getDomainConfig().getClusterConfigMap().get( clusterName );
83         if ( clusterConfig == null )
84         {
85             throw new IllegalArgumentException JavaDoc( "" + clusterName );
86         }
87         
88         final String JavaDoc configName = clusterConfig.getReferencedConfigName();
89         
90         debug( "creating server:\n" + (optional == null ? "null" : MapUtil.toString( optional )) );
91         final ObjectName JavaDoc serverObjectName =
92             super.create( serverName, nodeAgentName, configName, optional );
93         
94         // now create a reference.
95
debug( "created server: " + serverName + " in cluster " + clusterName +
96             "with node agent " + nodeAgentName );
97         final Map JavaDoc<String JavaDoc,String JavaDoc> options = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
98         options.put( ServerRefConfigCR.ENABLED_KEY, "true" );
99         options.put( ServerRefConfigCR.LB_ENABLED_KEY, "" + getLBEnabled(clusterConfig) );
100         debug( "creating reference" );
101         final ServerRefConfig refConfig =
102             clusterConfig.createServerRefConfig( serverName, options );
103         debug( "created reference" );
104         
105         // Now wait until internal code mutates the STANDALONE_SERVER_CONFIG into
106
// a CLUSTERED_SERVER_CONFIG
107
final String JavaDoc props = Util.makeRequiredProps( XTypes.CLUSTERED_SERVER_CONFIG, serverName );
108         Set JavaDoc<ClusteredServerConfig> s = null;
109         while( (s = getQueryMgr().queryPropsSet( props )).size() == 0 )
110         {
111             try
112             {
113                 Thread.sleep( 20 );
114             }
115             catch( InterruptedException JavaDoc e )
116             {
117                 throw new RuntimeException JavaDoc( e );
118             }
119         }
120             
121         return Util.getObjectName( s.iterator().next() );
122     }
123
124
125         private ClusterConfig
126     findClusterForClusteredServer( final String JavaDoc clusteredServerName )
127     {
128         // find the cluster referencing this server
129
ClusterConfig clusterConfig = null;
130         
131         final Map JavaDoc<String JavaDoc,ClusterConfig> clusters = getDomainConfig().getClusterConfigMap();
132         for( final ClusterConfig cc : clusters.values() )
133         {
134             final Map JavaDoc<String JavaDoc,ServerRefConfig> servers = cc.getServerRefConfigMap();
135             
136             if ( servers.keySet().contains( clusteredServerName ) )
137             {
138                 clusterConfig = cc;
139                 break;
140             }
141         }
142         
143         return clusterConfig;
144     }
145     
146         protected void
147     removeByName(final String JavaDoc clusteredServerName)
148     {
149         // find the cluster referencing this server
150
final ClusterConfig clusterConfig =
151             findClusterForClusteredServer( clusteredServerName );
152         if ( clusterConfig == null )
153         {
154             throw new IllegalArgumentException JavaDoc( "" + clusteredServerName );
155         }
156         
157         // found the ClusterConfig...remove its reference to this server
158
clusterConfig.removeServerRefConfig( clusteredServerName );
159         
160         super.removeByName( clusteredServerName );
161     }
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
Popular Tags