KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > AppserverClusterViewFromCacheRepository


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  * AppserverClusterViewFromCacheRepository.java
26  *
27  * Created on August 2, 2005, 2:46 PM
28  */

29
30 package com.sun.enterprise.config.serverbeans;
31
32 import com.sun.enterprise.admin.util.IAdminConstants;
33 import com.sun.enterprise.config.ConfigContext;
34 import com.sun.enterprise.config.ConfigException;
35 import com.sun.enterprise.config.ConfigFactory;
36 import com.sun.enterprise.util.SystemPropertyConstants;
37 import java.util.HashMap JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  */

42 public class AppserverClusterViewFromCacheRepository {
43     
44     private final String JavaDoc myName;
45     private final ConfigContext domainCC;
46     public AppserverClusterViewFromCacheRepository(final String JavaDoc domainXmlUrl) throws ConfigException {
47         if (domainXmlUrl == null) {
48             throw new IllegalArgumentException JavaDoc("null_arg");
49         }
50         myName = System.getProperty(SystemPropertyConstants.SERVER_NAME);
51         final String JavaDoc url = domainXmlUrl;
52         domainCC = ConfigFactory.createConfigContext(url, true, true, false, false);
53     }
54
55     public ConfigContext getUnResolvedConfigContext(){
56         return domainCC;
57     }
58
59      public Map JavaDoc<String JavaDoc, JmsHost> getResolvedLocalJmsHostsInCluster(final String JavaDoc clusterName) throws ConfigException {
60          final Map JavaDoc<String JavaDoc, JmsHost> map = new HashMap JavaDoc<String JavaDoc, JmsHost> ();
61          final String JavaDoc myCluster = ClusterHelper.getClusterByName(domainCC, clusterName).getName();
62          final Server[] buddies = ServerHelper.getServersInCluster(domainCC, myCluster);
63          for (final Server as : buddies) {
64              final JmsHost copy = getResolvedJmsHost(as);
65              map.put(as.getName(), copy);
66          }
67          return map;
68     }
69
70
71     public Map JavaDoc<String JavaDoc, JmsHost> getResolvedLocalJmsHostsInMyCluster(final boolean includeMe) throws ConfigException {
72         final Map JavaDoc<String JavaDoc, JmsHost> map = new HashMap JavaDoc<String JavaDoc, JmsHost> ();
73         if (ServerHelper.isServerClustered(domainCC, myName)) {
74             final String JavaDoc myCluster = ClusterHelper.getClusterForInstance(domainCC, myName).getName();
75             final Server[] buddies = ServerHelper.getServersInCluster(domainCC, myCluster);
76             for (final Server as : buddies) {
77                 if (!includeMe && myName.equals(as.getName()))
78                     continue;
79                 final JmsHost copy = getResolvedJmsHost(as);
80                 map.put(as.getName(), copy);
81             }
82         }
83         return ( map );
84     }
85     
86     public JmsHost getResolvedJmsHost(final Server as) throws ConfigException{
87         final JmsHost jmsHost = getResolvedLocalJmsHostInServer(as);
88         final JmsHost copy = new JmsHost();
89         final String JavaDoc hostName = getNodeAgentHostName(as);
90         if (jmsHost != null) {
91              copy.setHost(hostName);
92              copy.setName(jmsHost.getName());
93              final String JavaDoc resolved = this.resolve(as.getName(), SystemPropertyConstants.unSystemProperty(jmsHost.getPort()));
94              copy.setPort(resolved);
95              copy.setAdminPassword(jmsHost.getAdminPassword());
96              copy.setAdminUserName(jmsHost.getAdminUserName());
97          }
98         
99         return copy;
100     }
101
102     public Map JavaDoc<String JavaDoc, JmsHost> getResolvedLocalJmsHostsInMyCluster() throws ConfigException {
103         return ( this.getResolvedLocalJmsHostsInMyCluster(false) );
104     }
105     
106     public Map JavaDoc<String JavaDoc, IiopListener[]> getResolvedIiopListenersInCluster(final boolean includeMe) throws ConfigException {
107         final Map JavaDoc<String JavaDoc, IiopListener[]> map = new HashMap JavaDoc<String JavaDoc, IiopListener[]> ();
108         if (ServerHelper.isServerClustered(domainCC, myName)) {
109             final String JavaDoc myCluster = ClusterHelper.getClusterForInstance(domainCC, myName).getName();
110             final Server[] buddies = ServerHelper.getServersInCluster(domainCC, myCluster);
111             for (final Server as : buddies) {
112                 if (!includeMe && myName.equals(as.getName()))
113                     continue;
114                 final IiopListener[] ls = getResolvedIiopListenersInServer(as);
115                 map.put(as.getName(), ls);
116             }
117         }
118         return ( map );
119     }
120
121     public Map JavaDoc<String JavaDoc, IiopListener[]> getResolvedIiopListenersInCluster() throws ConfigException {
122         return ( this.getResolvedIiopListenersInCluster(false) );
123     }
124     
125     private JmsHost getResolvedLocalJmsHostInServer(final Server server) throws ConfigException {
126         final Config cfg = ServerHelper.getConfigForServer(domainCC, server.getName());
127         final JmsService jmsService = cfg.getJmsService();
128         JmsHost jmsHost = null;
129         if (JMSServiceType.LOCAL.toString().equals(jmsService.getType())) {
130             jmsHost = getDefaultJmsHost(jmsService);
131         }
132         return ( jmsHost );
133     }
134     
135     private JmsHost getDefaultJmsHost(final JmsService jmsService) {
136         final JmsHost[] jmsHosts = jmsService.getJmsHost();
137         final String JavaDoc defaultHostName = jmsService.getDefaultJmsHost();
138         JmsHost defaultJmsHost = null;
139         for (final JmsHost h : jmsHosts) {
140             final String JavaDoc name = h.getName();
141             if (name.equals(defaultHostName)) {
142                 defaultJmsHost = h;
143                 break;
144             }
145         }
146         return ( defaultJmsHost );
147     }
148     public String JavaDoc getNodeAgentHostName(final Server as) throws ConfigException {
149         final NodeAgent na = NodeAgentHelper.getNodeAgentForServer(domainCC, as.getName());
150         final boolean dasShookHandsWithNodeAgent = NodeAgentHelper.hasNodeAgentRendezvousd(domainCC, na);
151         if (! dasShookHandsWithNodeAgent)
152             throw new RuntimeException JavaDoc("Error: NA: " + na.getName() + " has not rendezvous'ed with DAS");
153         final String JavaDoc naHost = NodeAgentHelper.getNodeAgentSystemConnector(domainCC, na.getName()).getElementPropertyByName(IAdminConstants.HOST_PROPERTY_NAME).getValue();
154         return ( naHost );
155     }
156     private IiopListener[] getResolvedIiopListenersInServer(final Server server) throws ConfigException {
157         final Config cfg = ServerHelper.getConfigForServer(domainCC, server.getName());
158         final IiopService iiop = cfg.getIiopService();
159         final IiopListener[] lsnrs = iiop.getIiopListener();
160         final IiopListener[] values = new IiopListener[lsnrs.length];
161         int i = 0;
162         for (final IiopListener lsnr : lsnrs) {
163             final IiopListener copy = new IiopListener(); //avoid side effecting, hence send only copy!
164
String JavaDoc resolved = resolve(server.getName(), SystemPropertyConstants.unSystemProperty(lsnr.getId()));
165             copy.setId(resolved);
166             resolved = resolve(server.getName(), SystemPropertyConstants.unSystemProperty(lsnr.getPort()));
167             copy.setPort(resolved);
168             resolved = resolve(server.getName(), SystemPropertyConstants.unSystemProperty(lsnr.getAddress()));
169             copy.setAddress(resolved);
170             System.out.println("Iiop Service Endpoint for Server: " + server.getName() + ", iiop-lsnr-id = " + copy.getId() + ", iiop-lsnr-port = " + copy.getPort() + ", iiop-lsnr-address = " + copy.getAddress());
171             values[i++] = copy;
172         }
173         return ( values );
174     }
175     private String JavaDoc resolve(final String JavaDoc server, final String JavaDoc value) throws ConfigException {
176         final PropertyResolver pr = new PropertyResolver(domainCC, server);
177         String JavaDoc resolved = pr.getPropertyValue(value, true);
178         if (resolved == null) // the property could not be resolved, returned what was passed
179
resolved = value;
180         return ( resolved );
181     }
182 }
183 enum JMSServiceType {
184     LOCAL,
185     REMOTE,
186     EMBEDDED
187 }
188
Popular Tags