KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Id: ConnectorConnectionPoolMBean.java,v 1.3 2005/12/25 03:42:15 tcfujii Exp $
26  * Author: Vishal Byakod (vb130608)
27  */

28
29 package com.sun.enterprise.admin.mbeans;
30
31 //jdk imports
32
import java.util.Properties JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37
38 //JMX imports
39
import javax.management.Attribute JavaDoc;
40 import javax.management.AttributeList JavaDoc;
41 import javax.management.ObjectName JavaDoc;
42 import javax.management.MBeanException JavaDoc;
43 import javax.management.MBeanServerFactory JavaDoc;
44 import javax.management.MBeanServer JavaDoc;
45 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
46 import javax.management.MBeanInfo JavaDoc;
47
48 //config imports
49
import com.sun.enterprise.config.ConfigContext;
50 import com.sun.enterprise.config.ConfigBeansFactory;
51 import com.sun.enterprise.config.ConfigException;
52 import com.sun.enterprise.config.serverbeans.SecurityMap;
53 import com.sun.enterprise.config.serverbeans.BackendPrincipal;
54 import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
55 import com.sun.enterprise.config.serverbeans.Resources;
56
57 import com.sun.enterprise.admin.MBeanHelper;
58 import com.sun.enterprise.admin.config.BaseConfigMBean;
59 import com.sun.enterprise.admin.config.ConfigBeanHelper;
60 import com.sun.enterprise.admin.server.core.AdminService;
61 import com.sun.enterprise.admin.server.core.jmx.SunoneInterceptor;
62 import com.sun.enterprise.config.serverbeans.ServerTags;
63 import com.sun.enterprise.admin.meta.MBeanRegistry;
64 import com.sun.enterprise.util.i18n.StringManager;
65
66 import com.sun.enterprise.admin.mbeanapi.IConnectorConnectionPoolMBean;
67
68 import com.sun.enterprise.admin.target.Target;
69 import com.sun.enterprise.admin.target.ConfigTarget;
70
71 public class ConnectorConnectionPoolMBean extends BaseConfigMBean
72     implements IConnectorConnectionPoolMBean
73 {
74     
75     private static final String JavaDoc USER_NAME = "user_name";
76     private static final String JavaDoc PASSWORD = "password";
77     private static final String JavaDoc NAME = "name";
78     private static final String JavaDoc PRINCIPAL = "principal";
79     private static final String JavaDoc USER_GROUP = "user_group";
80     private static final String JavaDoc POOL_NAME = "pool_name";
81     private static final String JavaDoc MAP_NAME = "map_name";
82     private static final String JavaDoc VERBOSE = "verbose";
83     
84     private static final String JavaDoc ADD_PRINCIPALS = "add_principals";
85     private static final String JavaDoc REMOVE_PRINCIPALS = "remove_principals";
86     private static final String JavaDoc ADD_USER_GROUPS = "add_user_groups";
87     private static final String JavaDoc REMOVE_USER_GROUPS = "remove_user_groups";
88     
89     //operations
90
private static final String JavaDoc CREATE_SECURITY_MAP = "createSecurityMap";
91     private static final String JavaDoc CREATE_BACKEND_PRINCIPAL
92                                                     ="createBackendPrincipal";
93     private static final String JavaDoc GET_SECURITY_MAP = "getSecurityMap";
94     private static final String JavaDoc GET_SECURITY_MAP_BY_NAME = "getSecurityMapByName";
95     private static final String JavaDoc GET_BACKEND_PRINCIPAL = "getBackendPrincipal";
96     private static final String JavaDoc GET_POOL_BY_NAME
97                                         = "getConnectorConnectionPoolByName";
98     private static final String JavaDoc DEFAULT_TARGET = "domain";
99     private static final String JavaDoc GET_CONNECTOR_POOLS
100                                                 = "getConnectorConnectionPool";
101     
102     private static final String JavaDoc CONFIG = "config";
103     private static final String JavaDoc POOL_TYPE = "connector-connection-pool";
104     private static final String JavaDoc MAP_TYPE = "security-map";
105     private static final String JavaDoc RES_TYPE = "resources";
106    
107     // Exception strings....
108
private static final String JavaDoc POOL_DOES_NOT_EXISTS
109                                     ="admin.mbeans.ccpmb.pool_does_not_exists";
110     private static final String JavaDoc MAP_DOES_NOT_EXISTS
111                                     ="admin.mbeans.ccpmb.map_does_not_exists";
112     private static final String JavaDoc MAP_NAME_EXISTS
113                                         ="admin.mbeans.ccpmb.map_name_exists";
114     private static final String JavaDoc PRINCIPAL_USERGPS_NULL
115                                 ="admin.mbeans.ccpmb.principals_usergroups_null";
116     private static final String JavaDoc PRINCIPAL_EXISTS
117                                         ="admin.mbeans.ccpmb.principal_exists";
118     private static final String JavaDoc USERGROUP_EXISTS
119                                         ="admin.mbeans.ccpmb.usergroup_exists";
120     private static final String JavaDoc SAME_PRINCIPAL_VALUES
121                                     ="admin.mbeans.ccpmb.same_principal_values";
122     private static final String JavaDoc SAME_USERGROUP_VALUES
123                                     ="admin.mbeans.ccpmb.same_usergroup_values";
124     private static final String JavaDoc PRINCIPAL_DOES_NOT_EXISTS
125                                 ="admin.mbeans.ccpmb.principal_does_not_exists";
126     private static final String JavaDoc USERGROUP_DOES_NOT_EXISTS
127                                 ="admin.mbeans.ccpmb.usergroup_does_not_exists";
128     private static final String JavaDoc OPERATION_NOT_SUPPORTED
129                                 ="admin.mbeans.ccpmb.operation_not_supported";
130     private static final String JavaDoc USER_NAME_NULL
131                                         ="admin.mbeans.ccpmb.user_name_null";
132     private static final String JavaDoc PRINCIPALS_USERGROUPS_NULL
133                                 ="admin.mbeans.ccpmb.principals_usergroups_will_be_null";
134     
135     private static StringManager localStrings =
136     StringManager.getManager( ConnectorConnectionPoolMBean.class );
137    
138     
139    /**
140    */

141     
142     public ConnectorConnectionPoolMBean() {
143         super();
144         
145     }
146
147     public void createSecurityMap(AttributeList JavaDoc attrList)
148             throws Exception JavaDoc{
149         // Overriding the BaseConfigMBean operation so that this operation is
150
// not called by the user, as creation of security map requires
151
// backend-principal username and password also.
152
String JavaDoc msg = localStrings.getString(OPERATION_NOT_SUPPORTED);
153         throw new Exception JavaDoc(msg);
154     }
155       
156     
157     /* Creates a security map for the specified connection pool.
158       * @param AttributeList - this list contains all the parameters to create
159       * a new map
160      */

161      public ObjectName JavaDoc createSecurityMap(AttributeList JavaDoc attrList,String JavaDoc userName,
162      String JavaDoc password ,String JavaDoc tgtName ) throws MBeanException JavaDoc {
163         ObjectName JavaDoc mbean = null;
164         String JavaDoc poolName = null;
165         String JavaDoc mapname = null;
166         
167         try {
168             if (tgtName == null || tgtName.equals("")) tgtName = DEFAULT_TARGET;
169             
170             MBeanServer JavaDoc server = getMBeanServer();
171             String JavaDoc principals[] = null;
172             String JavaDoc usergroups[] = null;
173             
174             if(attrList != null){
175                 int s = attrList.size();
176                 for(int i=0;i<s;i++){
177                     try {
178                         Attribute JavaDoc attribute =(Attribute JavaDoc)attrList.get(i);
179                         if(isAttrNameMatch(attribute, NAME)){
180                              mapname =(String JavaDoc) attribute.getValue();
181                              continue;
182                          }
183                          if (isAttrNameMatch(attribute, PRINCIPAL))
184                             principals = (String JavaDoc[])attribute.getValue();
185                          if (isAttrNameMatch(attribute, USER_GROUP))
186                             usergroups =(String JavaDoc[]) attribute.getValue();
187                          if (isAttrNameMatch(attribute, POOL_NAME))
188                             poolName = (String JavaDoc)attribute.getValue();
189                     } catch(Exception JavaDoc e){
190                         throw new Exception JavaDoc("failed while getting attribute" +
191                         "names and values");
192                     }
193                 }
194             }
195             
196            if(!doesPoolNameExists(poolName)){
197                 String JavaDoc msg = localStrings.getString(POOL_DOES_NOT_EXISTS,poolName);
198                 throw new Exception JavaDoc(msg);
199             }
200         
201             if(doesMapNameExists(poolName,mapname)){
202                 String JavaDoc msg = localStrings.getString(MAP_NAME_EXISTS
203                                                         ,mapname,poolName);
204                 throw new Exception JavaDoc(msg);
205                 
206             }
207             
208             //check if backend-principal user name is null
209
if(userName == null){
210                 String JavaDoc msg = localStrings.getString(USER_NAME_NULL);
211                 throw new Exception JavaDoc(msg);
212             }
213                
214             // check if both principals and usergroups are null .If yes throw
215
// exception since atleast one of these is required.
216
if(principals == null && usergroups == null){
217                 String JavaDoc msg = localStrings.getString(PRINCIPAL_USERGPS_NULL);
218                 throw new Exception JavaDoc(msg);
219             }
220             
221             //get all the security maps for this pool.....
222
ObjectName JavaDoc[] maps = getAllSecurityMapsForPool(poolName);
223             
224             if(principals != null){
225                 for(int i=0;i<principals.length;i++){
226                     if (isPrincipalExisting(principals[i],maps)){
227                         String JavaDoc msg = localStrings.getString
228                         (PRINCIPAL_EXISTS,principals[i],poolName);
229                         throw new Exception JavaDoc(msg);
230                         
231                     }
232                 }
233             }
234             if(usergroups != null){
235                 for(int i=0;i<usergroups.length;i++){
236                     if (isUserGroupExisting(usergroups[i],maps)){
237                         String JavaDoc msg = localStrings.getString
238                         (USERGROUP_EXISTS,usergroups[i],poolName);
239                         throw new Exception JavaDoc(msg);
240                     }
241                 }
242             }
243          
244            //This is a temporary fix for 8.0 pe.Currently there is no way you
245
// can create a security map and its a backend principal in a atomic
246
// manner . This would need two invoke operations on two different MBeans
247
// and hence events would be fired as soon as the first invoke operation
248
// completes which causes a NPE being thrown in the backend while accessing
249
// the backend principal.
250
//The direct call to config beans would be used until we find a better solution
251
// to handle this problem.
252
ConfigContext serverContext = getConfigContext();
253              com.sun.enterprise.config.serverbeans.Resources resourcesBean =
254                 (Resources)ConfigBeansFactory.getConfigBeanByXPath(serverContext,
255                                             ServerXPathHelper.XPATH_RESOURCES);
256             com.sun.enterprise.config.serverbeans.ConnectorConnectionPool connPool =
257                     resourcesBean.getConnectorConnectionPoolByName(poolName);
258             com.sun.enterprise.config.serverbeans.BackendPrincipal backEndPrincipal
259                 = new com.sun.enterprise.config.serverbeans.BackendPrincipal();
260             
261             backEndPrincipal.setUserName(userName);
262             
263             if(password != null)
264                 backEndPrincipal.setPassword(password);
265             
266             com.sun.enterprise.config.serverbeans.SecurityMap securityMap =
267                 new com.sun.enterprise.config.serverbeans.SecurityMap();
268             
269             if(backEndPrincipal != null)
270                 securityMap.setBackendPrincipal(backEndPrincipal);
271             
272             if(mapname !=null)
273                 securityMap.setName(mapname);
274             
275             if(principals != null)
276                 securityMap.setPrincipal(principals);
277             
278             if(usergroups != null)
279               securityMap.setUserGroup(usergroups);
280             
281             connPool.addSecurityMap(securityMap);
282        }/*catch (MBeanException me){
283        throw me;
284     }*/
catch (Exception JavaDoc e) {
285             e.printStackTrace();
286             throw new MBeanException JavaDoc(e);
287         }
288         return mbean;
289     }
290     
291     /* Updates a existing Security Map of the specified connection pool.
292      * @param AttributeList - this list contains all the parameters to update a
293      * existing map.
294      */

295  
296       public boolean updateSecurityMap(AttributeList JavaDoc attrList,String JavaDoc tgtName )
297     throws MBeanException JavaDoc {
298         
299         String JavaDoc mapname = null;
300         String JavaDoc poolname = null;
301         String JavaDoc username = null;
302         String JavaDoc password = null;
303         String JavaDoc[] addPrincipals = null;
304         String JavaDoc[] addUserGroups = null;
305         String JavaDoc[] removePrincipals = null;
306         String JavaDoc[] removeUserGroups = null;
307         boolean status = false;
308         //ConfigContext ctx = getConfigContext();
309

310         try {
311         if (tgtName == null || tgtName.equals("")) tgtName = DEFAULT_TARGET;
312                     
313             //get all the values from the attribute list ...
314
if(attrList != null){
315                 int s = attrList.size();
316                 for(int i=0;i<s;i++){
317                     try{
318                         Attribute JavaDoc attribute =(Attribute JavaDoc)attrList.get(i);
319                         if((isAttrNameMatch(attribute, POOL_NAME)))
320                             poolname = (String JavaDoc)attribute.getValue();
321                         if((isAttrNameMatch(attribute, NAME)))
322                             mapname = (String JavaDoc)attribute.getValue();
323                         if((isAttrNameMatch(attribute, USER_NAME)))
324                             username = (String JavaDoc)attribute.getValue();
325                         if((isAttrNameMatch(attribute, PASSWORD)))
326                             password = (String JavaDoc)attribute.getValue();
327                         if((isAttrNameMatch(attribute, ADD_PRINCIPALS)))
328                             addPrincipals =
329                                     getOptionsList((String JavaDoc)attribute.getValue());
330                         if((isAttrNameMatch(attribute, ADD_USER_GROUPS)))
331                             addUserGroups =
332                                     getOptionsList((String JavaDoc)attribute.getValue());
333                         if((isAttrNameMatch(attribute, REMOVE_PRINCIPALS)))
334                             removePrincipals =
335                                     getOptionsList((String JavaDoc)attribute.getValue());
336                         if((isAttrNameMatch(attribute, REMOVE_USER_GROUPS)))
337                             removeUserGroups =
338                                     getOptionsList((String JavaDoc)attribute.getValue());
339                     }catch(Exception JavaDoc e){
340                         e.printStackTrace();
341                     }
342                 }
343             }
344             if(!doesPoolNameExists(poolname)){
345                 String JavaDoc msg = localStrings.getString(POOL_DOES_NOT_EXISTS,poolname);
346                 throw new Exception JavaDoc(msg);
347             }
348             
349             if(!doesMapNameExists(poolname,mapname)){
350                 String JavaDoc msg = localStrings.getString(MAP_DOES_NOT_EXISTS
351                                                             ,mapname,poolname);
352                 throw new Exception JavaDoc(msg);
353             }
354             //get all the security maps for this pool.....
355
ObjectName JavaDoc[] maps = getAllSecurityMapsForPool(poolname);
356                         
357             //check if addPrincipals and removePrincipals have the same value
358
if(addPrincipals != null && removePrincipals != null){
359                 for(int i=0; i < addPrincipals.length ; i++) {
360                     for (int j=0; j < removePrincipals.length; j++) {
361                         if(removePrincipals[j].equals(addPrincipals[i])){
362                             String JavaDoc msg = localStrings.getString(
363                                     SAME_PRINCIPAL_VALUES,addPrincipals[i]);
364                             throw new Exception JavaDoc(msg);
365                         }
366                                        
367                     }
368                 }
369             }
370             
371             //check if addUserGroups and removeUserGroups have the same value
372
if(addUserGroups != null && removeUserGroups != null){
373                 for(int i=0; i < addUserGroups.length ; i++) {
374                     for (int j=0; j < removeUserGroups.length; j++) {
375                         if(removeUserGroups[j].equals(addUserGroups[i])){
376                             String JavaDoc msg = localStrings.getString(
377                             SAME_USERGROUP_VALUES,addUserGroups[i]);
378                             throw new Exception JavaDoc(msg);
379                         }
380                                        
381                     }
382                 }
383             }
384             // make sure that the principals to be added are not existing in any map ...
385
if(addPrincipals != null){
386                 for(int i=0;i<addPrincipals.length;i++){
387                     if (isPrincipalExisting(addPrincipals[i],maps)){
388                         String JavaDoc msg = localStrings.getString(PRINCIPAL_EXISTS,
389                                                     addPrincipals[i],poolname);
390                         throw new Exception JavaDoc(msg);
391                     }
392                 }
393             }
394             // make sure that the user groups to be added are not existing in any map ...
395
if(addUserGroups != null){
396                 for(int i=0;i<addUserGroups.length;i++){
397                     if (isUserGroupExisting(addUserGroups[i],maps)){
398                      String JavaDoc msg = localStrings.getString(USERGROUP_EXISTS
399                         ,addUserGroups[i],poolname);
400                         throw new Exception JavaDoc(msg);
401                     }
402                 }
403             }
404             //get a reference to the MBean server ...
405
MBeanServer JavaDoc server = getMBeanServer();
406             ObjectName JavaDoc mbean = getSecurityMapObjectName(mapname,poolname);
407             String JavaDoc existingPrincipals[] = null;
408             String JavaDoc existingUserGroups[] = null;
409             
410             existingPrincipals =(String JavaDoc[]) server.getAttribute(mbean,PRINCIPAL);
411             existingUserGroups =(String JavaDoc[]) server.getAttribute(mbean,USER_GROUP);
412            
413             ArrayList JavaDoc source = null;
414             ArrayList JavaDoc source1 = null;
415             
416             if(existingPrincipals != null){
417                 source = new ArrayList JavaDoc(existingPrincipals.length);
418                 for(int i=0; i<existingPrincipals.length ; i++)
419                     source.add(existingPrincipals[i]);
420             }
421             if(existingUserGroups != null){
422                 source1 = new ArrayList JavaDoc(existingUserGroups.length);
423                 for(int i=0; i<existingUserGroups.length ; i++)
424                     source1.add(existingUserGroups[i]);
425             }
426             //check if there is any invalid principal in removePrincipals.
427
if(removePrincipals != null){
428                 for(int i=0;i<removePrincipals.length;i++){
429                     String JavaDoc s = removePrincipals[i];
430                     if (!source.contains(s)){
431                         String JavaDoc msg =
432                             localStrings.getString(PRINCIPAL_DOES_NOT_EXISTS,s,poolname);
433                         throw new Exception JavaDoc(msg);
434                     }
435                 }
436             }
437             //check if there is any invalid usergroup in removeUserGroups.
438
if(removeUserGroups != null){
439                 for(int i=0;i<removeUserGroups.length;i++){
440                     String JavaDoc s = removeUserGroups[i];
441                     if (!source1.contains(s)){
442                         String JavaDoc msg =
443                         localStrings.getString(USERGROUP_DOES_NOT_EXISTS,s,poolname);
444                         throw new Exception JavaDoc(msg);
445                     }
446                 }
447             }
448            
449             //FIX : Bug 4914883.
450
//The user should not delete all principals and usergroups in the map.
451
// Atleast one principal or usergroup must exists.
452

453             if(addPrincipals == null && addUserGroups == null ) {
454                 boolean principalsEmpty = false;
455                 boolean userGroupsEmpty = false;
456                 
457                 if(removePrincipals == null && existingPrincipals.length==0)
458                     principalsEmpty = true;
459                 if(removeUserGroups == null && existingUserGroups.length==0)
460                     userGroupsEmpty = true;
461                 
462                 if (( removePrincipals != null )&&
463                             (removePrincipals.length== existingPrincipals.length))
464                     principalsEmpty = true;
465                 
466                 if (( removeUserGroups != null ) &&
467                             (removeUserGroups.length== existingUserGroups.length))
468                     userGroupsEmpty = true;
469                 if (userGroupsEmpty && principalsEmpty) {
470                    String JavaDoc msg = localStrings.getString(PRINCIPALS_USERGROUPS_NULL);
471                    throw new Exception JavaDoc(msg);
472
473                 }
474             }
475          
476             //add principals to the source arraylist.
477
if(addPrincipals != null){
478                 for(int i=0; i < addPrincipals.length; i++) {
479                     String JavaDoc s= addPrincipals[i];
480                     if (!source.contains(s))
481                       source.add(s);
482                     else{
483                         String JavaDoc msg = localStrings.getString(PRINCIPAL_EXISTS
484                                                                 ,s,poolname);
485                         throw new Exception JavaDoc(msg);
486                     }
487                 }
488             }
489             //removing principals from source arraylist.
490
if(removePrincipals != null){
491                 for(int i=0; i <removePrincipals.length ;i++) {
492                     String JavaDoc s = removePrincipals[i];
493                     source.remove(s);
494                 }
495             }
496
497             String JavaDoc newPrincipals[] = new String JavaDoc[source.size()];
498             for(int i=0; i< source.size(); i++)
499                 newPrincipals[i]=(String JavaDoc) source.get(i);
500                         
501             //adding user-groups....
502
if(addUserGroups != null){
503                 for(int i=0; i < addUserGroups.length; i++) {
504                     String JavaDoc s= addUserGroups[i];
505                     if (!source1.contains(s))
506                         source1.add(s);
507                     else{
508                         String JavaDoc msg = localStrings.getString
509                         (USERGROUP_EXISTS,s,poolname);
510                         throw new Exception JavaDoc(msg);
511                     }
512                 }
513             }
514             //removing user-groups....
515

516             if(removeUserGroups != null){
517                 for(int i=0; i <removeUserGroups.length ;i++) {
518                     String JavaDoc s = removeUserGroups[i];
519                     source1.remove(s);
520                 }
521             }
522        
523             String JavaDoc newUserGroups[] = new String JavaDoc[source1.size()];
524             for(int i=0; i< source1.size(); i++)
525                 newUserGroups[i]=(String JavaDoc) source1.get(i);
526             
527             
528             //setting the updated principal user-group arrays....
529
Attribute JavaDoc princ =null;
530              Attribute JavaDoc ug = null;
531              if(newPrincipals != null){
532                 princ = new Attribute JavaDoc(PRINCIPAL,newPrincipals);
533                 server.setAttribute(mbean,princ);
534              }
535              if(newUserGroups != null){
536                 ug = new Attribute JavaDoc(USER_GROUP,newUserGroups);
537                 server.setAttribute(mbean,ug);
538              }
539              
540             //updating the backend-principal.......
541
//get the backend principal for the given security map and pool...
542
ObjectName JavaDoc backendPrincipal =(ObjectName JavaDoc) getMBeanServer().invoke(
543              mbean, GET_BACKEND_PRINCIPAL,null,null);
544             
545              if(username != null){
546                 if(!((String JavaDoc)(server.getAttribute(backendPrincipal,
547                                         USER_NAME))).equals(username))
548                     server.setAttribute(backendPrincipal,
549                                             new Attribute JavaDoc(USER_NAME,username));
550              }
551              
552              if(password != null){
553                 if(!((String JavaDoc)(server.getAttribute(backendPrincipal,
554                                             PASSWORD))).equals(password))
555                     server.setAttribute(backendPrincipal,
556                                     new Attribute JavaDoc(PASSWORD,password));
557              }
558              status = true;
559         } catch (Exception JavaDoc e) {
560             status = false;
561             e.printStackTrace();
562             throw new MBeanException JavaDoc(e);
563         }
564         return status;
565     }
566  
567  
568     
569     public ArrayList JavaDoc listSecurityMap(String JavaDoc mapName,Boolean JavaDoc verb,String JavaDoc poolName,
570                     String JavaDoc tgtName) throws MBeanException JavaDoc{
571         
572         ObjectName JavaDoc mbean = null;
573         StringBuffer JavaDoc strf = new StringBuffer JavaDoc();
574         boolean verbose = false;
575         ArrayList JavaDoc list = new ArrayList JavaDoc();
576
577         try {
578             if (tgtName == null || tgtName.equals("")) tgtName = DEFAULT_TARGET;
579             if(!doesPoolNameExists(poolName)){
580                 String JavaDoc msg = localStrings.getString(POOL_DOES_NOT_EXISTS,poolName);
581                 throw new Exception JavaDoc(msg);
582             }
583             if(mapName != null){
584                 if(!doesMapNameExists(poolName,mapName)){
585                     String JavaDoc msg = localStrings.getString(MAP_DOES_NOT_EXISTS
586                                                             ,mapName,poolName);
587                     throw new Exception JavaDoc(msg);
588                 }
589             }
590                 
591              
592              verbose = verb.booleanValue();
593              //get all the map object names...
594
ObjectName JavaDoc[] maps =(ObjectName JavaDoc[]) super.invoke(GET_SECURITY_MAP,null,null);
595              if(mapName == null && verbose){
596                  if(maps != null && maps.length >0){
597                     for(int i=0;i<maps.length;i++){
598                         String JavaDoc map = (String JavaDoc)getMBeanServer().getAttribute(
599                                                                 maps[i],NAME);
600                         ObjectName JavaDoc mapRef =(ObjectName JavaDoc)super.invoke(
601                         GET_SECURITY_MAP_BY_NAME, new Object JavaDoc[]{map},
602                                         new String JavaDoc[] {"java.lang.String"});
603                         list.add(createMapInfo(mapRef));
604                                                           
605                     }
606                 }
607              }else if( mapName == null && !verbose ){
608                  if(maps != null && maps.length >0){
609                     for(int i=0;i<maps.length;i++){
610                         String JavaDoc map = (String JavaDoc)
611                                 getMBeanServer().getAttribute(maps[i],NAME);
612                         AttributeList JavaDoc attr = new AttributeList JavaDoc();
613                         if(map != null)
614                             attr.add(new Attribute JavaDoc(NAME,map));
615                  
616                         //print the map names .....
617
ObjectName JavaDoc mapRef =(ObjectName JavaDoc)
618                          super.invoke(GET_SECURITY_MAP_BY_NAME,new Object JavaDoc[]{map},
619                                            new String JavaDoc[] {"java.lang.String"});
620                          String JavaDoc mapname = (String JavaDoc)getMBeanServer().getAttribute(mapRef,NAME);
621                          list.add(new String JavaDoc[]{mapname,null,null,null,null});
622                     }
623                  }
624              }else {
625                  // map name is not null, print the map details if verbose is true...
626
ObjectName JavaDoc mapRef =(ObjectName JavaDoc)super.invoke(GET_SECURITY_MAP_BY_NAME,
627                         new Object JavaDoc[]{mapName}, new String JavaDoc[] {"java.lang.String"});
628                  if(mapRef != null) {
629                      if (verbose) {
630                         list.add(createMapInfo(mapRef));
631                      } else {
632                          list.add(new String JavaDoc[]{mapName,null,null,null,null});
633                      }
634                  }
635                            
636              }
637          }catch (Exception JavaDoc e) {
638             e.printStackTrace();
639             throw new MBeanException JavaDoc(e);
640         }
641
642         return list;
643     }
644     
645
646     // -----------------Set of Helper methods ---------------------------- //
647

648     // Gives the attribute list for the mapname and poolname.
649
public AttributeList JavaDoc getAttributes(String JavaDoc mapName,String JavaDoc poolName)
650     throws Exception JavaDoc {
651         String JavaDoc mapname = null;
652         String JavaDoc[] existingPrincipals = null;
653         String JavaDoc[] existingUserGroups = null;
654         String JavaDoc username = null;
655         String JavaDoc password = null;
656         
657         if(mapName != null){
658             if(!doesMapNameExists(poolName,mapName)){
659                 String JavaDoc msg = localStrings.getString(MAP_DOES_NOT_EXISTS
660                                                             ,mapName,poolName);
661                 throw new Exception JavaDoc(msg);
662             }
663         }
664         
665          ObjectName JavaDoc mapRef =(ObjectName JavaDoc)super.invoke(
666                 GET_SECURITY_MAP_BY_NAME, new Object JavaDoc[]{mapName},
667                 new String JavaDoc[] {"java.lang.String"});
668          if(mapRef != null) {
669             mapname = (String JavaDoc)getMBeanServer().getAttribute(mapRef,NAME);
670             existingPrincipals =
671                 (String JavaDoc[])getMBeanServer().getAttribute(mapRef,PRINCIPAL);
672             existingUserGroups =
673                 (String JavaDoc[])getMBeanServer().getAttribute(mapRef,USER_GROUP);
674          }
675             
676          ObjectName JavaDoc backEndPrincipal = (ObjectName JavaDoc)getMBeanServer().invoke(
677          mapRef,GET_BACKEND_PRINCIPAL,null,null);
678          if(backEndPrincipal != null){
679             username = (String JavaDoc)
680                    getMBeanServer().getAttribute(backEndPrincipal,USER_NAME);
681             password = (String JavaDoc)
682                     getMBeanServer().getAttribute(backEndPrincipal,PASSWORD);
683          }
684          //set the attributes......
685
AttributeList JavaDoc attributes = new AttributeList JavaDoc();
686          if(mapname != null)
687              attributes.add(new Attribute JavaDoc(NAME,mapname));
688          if(existingPrincipals != null)
689              attributes.add(new Attribute JavaDoc(PRINCIPAL,existingPrincipals));
690          if(existingUserGroups != null)
691              attributes.add(new Attribute JavaDoc(USER_GROUP,existingUserGroups));
692          if(username != null)
693              attributes.add(new Attribute JavaDoc(USER_NAME,username));
694          if(password != null)
695              attributes.add(new Attribute JavaDoc(PASSWORD,password));
696          
697          return attributes;
698     }
699     
700     private ObjectName JavaDoc getConnectorConnObjectName(String JavaDoc poolName)throws Exception JavaDoc{
701         return m_registry.getMbeanObjectName(POOL_TYPE,
702                             new String JavaDoc[]{getDomainName(),poolName,CONFIG});
703     }
704       
705     private ObjectName JavaDoc getSecurityMapObjectName(String JavaDoc mapName,String JavaDoc poolName)
706     throws Exception JavaDoc{
707         return m_registry.getMbeanObjectName(MAP_TYPE,
708                     new String JavaDoc[]{getDomainName(),poolName,mapName,CONFIG});
709     }
710     private boolean isPrincipalExisting(String JavaDoc principal,ObjectName JavaDoc[] maps)
711     throws Exception JavaDoc{
712         boolean exists =false;
713         String JavaDoc[] existingPrincipals = null;
714
715         if (maps!= null && maps.length > 0){
716             for (int k=0; k<maps.length && !exists ; k++){
717                 existingPrincipals =
718                 (String JavaDoc[])getMBeanServer().getAttribute(maps[k],PRINCIPAL);
719                     if(existingPrincipals != null && principal != null){
720                         for(int i=0; i < existingPrincipals.length ; i++) {
721                             if(existingPrincipals[i].equals(principal)){
722                                 exists = true;
723                                 break;
724                             }
725                         }
726                     }
727             }
728         }
729     return exists ;
730     }
731
732     private boolean isUserGroupExisting(String JavaDoc usergroup,ObjectName JavaDoc[] maps)
733     throws Exception JavaDoc {
734         boolean exists =false;
735         String JavaDoc[] existingUserGroups = null;
736         if (maps!= null && maps.length > 0){
737             for (int k=0; k<maps.length && !exists ; k++){
738                 existingUserGroups =
739                 (String JavaDoc[])getMBeanServer().getAttribute(maps[k],USER_GROUP);
740                     if(existingUserGroups != null && usergroup != null){
741                         for(int i=0; i < existingUserGroups.length ; i++) {
742                         if(existingUserGroups[i].equals(usergroup)){
743                             exists = true;
744                             break;
745                         }
746                     }
747                 }
748           }
749         }
750     return exists ;
751     }
752
753     protected ObjectName JavaDoc getConfigMBean(Target target) throws Exception JavaDoc {
754         ConfigTarget ct = target.getConfigTarget();
755         return new ObjectName JavaDoc(
756                 ct.getTargetObjectName(new String JavaDoc[] {getDomainName()}));
757     }
758       
759     private ObjectName JavaDoc[] getAllSecurityMapsForPool(String JavaDoc poolName )throws Exception JavaDoc{
760          ObjectName JavaDoc poolObj = getConnectorConnObjectName(poolName);
761             return (ObjectName JavaDoc[])getMBeanServer().invoke(poolObj,
762                                         GET_SECURITY_MAP, null, null);
763     }
764     
765     private boolean doesPoolNameExists(String JavaDoc poolName )throws Exception JavaDoc {
766        //check if the poolname exists.If it does not then throw an exception.
767
ObjectName JavaDoc resObj = m_registry.getMbeanObjectName(RES_TYPE,
768                             new String JavaDoc[]{getDomainName(),CONFIG});
769         ObjectName JavaDoc[] pools =(ObjectName JavaDoc[])getMBeanServer().invoke(resObj,
770                                             GET_CONNECTOR_POOLS,null,null);
771         boolean doesPoolExists = false;
772         if (pools!= null && pools.length > 0){
773             for (int i=0; i<pools.length; i++){
774                 String JavaDoc pool = (String JavaDoc)getMBeanServer().getAttribute(pools[i],NAME);
775                 if (pool.equals(poolName))
776                         doesPoolExists = true;
777             }
778         }
779         return doesPoolExists;
780     }
781     
782     private boolean doesMapNameExists(String JavaDoc poolName,String JavaDoc mapname )throws Exception JavaDoc{
783         //check if the mapname exists for the given pool name..
784
ObjectName JavaDoc poolObj = getConnectorConnObjectName(poolName);
785         ObjectName JavaDoc[] maps =(ObjectName JavaDoc[])getMBeanServer().invoke(poolObj,
786                                     GET_SECURITY_MAP, null, null);
787
788         boolean doesMapNameExists = false;
789         if (maps!= null && maps.length > 0){
790                 for (int i=0; i<maps.length; i++){
791                     String JavaDoc mapName =
792                             (String JavaDoc)getMBeanServer().getAttribute(maps[i],NAME);
793                     if (mapName.equals(mapname))
794                         doesMapNameExists = true;
795                 }
796         }
797         return doesMapNameExists;
798     }
799     
800     private String JavaDoc[] createMapInfo(ObjectName JavaDoc mapRef )throws Exception JavaDoc{
801          String JavaDoc[] existingPrincipals = null;
802          String JavaDoc[] existingUserGroups = null;
803          StringBuffer JavaDoc buf1= new StringBuffer JavaDoc();
804          StringBuffer JavaDoc buf2= new StringBuffer JavaDoc();
805          String JavaDoc mapname = null;
806          String JavaDoc username = null;
807          String JavaDoc password = null;
808                              
809         if(mapRef != null) {
810             mapname = (String JavaDoc)getMBeanServer().getAttribute(mapRef,NAME);
811             existingPrincipals =
812                     (String JavaDoc[])getMBeanServer().getAttribute(mapRef,PRINCIPAL);
813             existingUserGroups =
814                     (String JavaDoc[])getMBeanServer().getAttribute(mapRef,USER_GROUP);
815             if(existingPrincipals != null){
816                 for(int j=0;j<existingPrincipals.length;j++){
817                     buf1.append(existingPrincipals[j]);
818                     buf1.append(",");
819                 }
820             }
821             if(existingUserGroups != null){
822                 for(int j=0;j<existingUserGroups.length;j++){
823                     buf2.append(existingUserGroups[j]);
824                     buf2.append(",");
825                 }
826             }
827             ObjectName JavaDoc backEndPrincipal = (ObjectName JavaDoc)getMBeanServer().invoke(
828                                 mapRef,GET_BACKEND_PRINCIPAL,null,null);
829             if(backEndPrincipal != null){
830                 username =
831                 (String JavaDoc)getMBeanServer().getAttribute(backEndPrincipal,USER_NAME);
832                 password =
833                 (String JavaDoc)getMBeanServer().getAttribute(backEndPrincipal,PASSWORD);
834          
835             }
836         }
837         return new String JavaDoc[]{mapname,buf1.toString(),buf2.toString(),username,password};
838    
839      }
840     
841     private String JavaDoc[] getOptionsList(Object JavaDoc sOptions){
842         StringTokenizer JavaDoc optionTokenizer = new StringTokenizer JavaDoc((String JavaDoc)sOptions,",");
843         int size = optionTokenizer.countTokens();
844         String JavaDoc [] sOptionsList = new String JavaDoc[size];
845         for (int ii=0; ii<size; ii++){
846             sOptionsList[ii] = optionTokenizer.nextToken();
847         }
848         return sOptionsList;
849    }
850     
851     protected MBeanServer JavaDoc getMBeanServer() {
852         return com.sun.enterprise.admin.common.MBeanServerFactory.getMBeanServer();
853     }
854
855     private static boolean isAttrNameMatch(Attribute JavaDoc attr, String JavaDoc name)
856     {
857         //FIXME: this code should be changed after FCS
858
// for now we supporting both "dashed" and "underscored" names
859
return attr.getName().replace('_','-').equals(name.replace('_','-'));
860     }
861
862     
863 }
864
Popular Tags