KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > policies > PolicyManager


1 package org.jacorb.orb.policies;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.omg.CORBA._PolicyManagerLocalBase;
24
25 import org.apache.avalon.framework.logger.*;
26
27 import java.util.*;
28
29 /**
30  * Implementation of the ORB-level policy management interface as per
31  * CORBA 2.6, p. 4-43 to 4-45:
32  *
33  * This PolicyManager has operations through which a set of Policies
34  * can be applied and the current overriding Policy settings can be
35  * obtained. Policies applied at the ORB level override any system
36  * defaults. The ORBís PolicyManager is obtained through an invocation
37  * of ORB::resolve_initial_references, specifying an identifier of
38  * "ORBPolicyManager."
39  *
40  * @author Gerald Brose, mailto:gerald.brose@acm.org
41  * @version $Id: PolicyManager.java,v 1.3 2004/05/06 12:40:00 nicolas Exp $
42  */

43
44 public class PolicyManager
45     extends _PolicyManagerLocalBase
46 {
47     private org.jacorb.orb.ORB orb;
48     private Map policy_overrides = null;
49     private Logger logger ;
50
51     /**
52      * public c'tor
53      */

54
55     public PolicyManager(org.jacorb.orb.ORB orb)
56     {
57         this.orb = orb;
58         policy_overrides = new HashMap();
59         logger = orb.getConfiguration().getNamedLogger("jacorb.orb.policies");
60     }
61
62     /**
63      * Returns a PolicyList containing the overridden Polices for the
64      * requested PolicyTypes. If the specified sequence is empty, all
65      * Policy overrides at this scope will be returned. If none of
66      * the requested PolicyTypes are overridden at the target
67      * PolicyManager, an empty sequence is returned. This accessor
68      * returns only those Policy overrides that have been set at the
69      * specific scope corresponding to the target PolicyManager (no
70      * evaluation is done with respect to overrides at other scopes).
71      *
72      * @param ts a sequence of overridden policy types identifying the
73      * policies that are to be retrieved.
74      * @return the list of overridden policies of the types specified by ts
75      */

76
77     public synchronized org.omg.CORBA.Policy JavaDoc[] get_policy_overrides(int[] ts)
78     {
79         if (ts == null)
80             throw new IllegalArgumentException JavaDoc("Argument may not be null");
81
82         if (ts.length == 0)
83         {
84             return (org.omg.CORBA.Policy JavaDoc[])policy_overrides.values().toArray( new org.omg.CORBA.Policy JavaDoc[]{});
85         }
86
87         Vector policyList = new Vector();
88
89         for (int i = 0; i < ts.length; i++ )
90         {
91             org.omg.CORBA.Policy JavaDoc p =
92                 (org.omg.CORBA.Policy JavaDoc)policy_overrides.get( new Integer JavaDoc( ts[i] ));
93             if (p != null)
94                 policyList.add(p);
95         }
96
97         org.omg.CORBA.Policy JavaDoc[] result =
98             (org.omg.CORBA.Policy JavaDoc[])policyList.toArray( new org.omg.CORBA.Policy JavaDoc[]{});
99
100         if (logger.isDebugEnabled())
101             logger.debug("get_policy_overrides returns " + result.length + " policies");
102
103         return result;
104             
105     }
106
107     /**
108      *
109      * Modifies the current set of overrides with the requested list
110      * of Policy overrides. The first parameter policies is a sequence
111      * of references to Policy objects. The second parameter set_add
112      * of type SetOverrideType indicates whether these policies should
113      * be added onto any other overrides that already exist
114      * (ADD_OVERRIDE) in the PolicyManager, or they should be added to
115      * a clean PolicyManager free of any other overrides
116      * (SET_OVERRIDE).
117      * <p>
118      * Invoking set_policy_overrides with an empty sequence of
119      * policies and a mode of SET_OVERRIDE removes all overrides from
120      * a PolicyManager. Only certain policies that pertain to the
121      * invocation of an operation at the client end can be overridden
122      * using this operation. Attempts to override any other policy
123      * will result in the raising of the CORBA::NO_PERMISSION
124      * exception. If the request would put the set of overriding
125      * policies for the target PolicyManager in an inconsistent state,
126      * no policies are changed or added, and the exception
127      * InvalidPolicies is raised. There is no evaluation of
128      * compatibility with policies set within other PolicyManagers.
129      *
130      * @param policies a sequence of Policy objects that are to be
131      * associated with the PolicyManager object.
132      *
133      * @param set_add whether the association is in addition to
134      * (ADD_OVERRIDE) or as a replacement of (SET_OVERRIDE) any
135      * existing overrides already associated with the PolicyManager
136      * object. If the value of this parameter is SET_OVERRIDE, the
137      * supplied policies completely replace all existing overrides
138      * associated with the PolicyManager object. If the value of this
139      * parameter is ADD_OVERRIDE, the supplied policies are added to
140      * the existing overrides associated with the PolicyManager
141      * object, except that if a supplied Policy object has the same
142      * PolicyType value as an existing override, the supplied Policy
143      * object replaces the existing override.
144      *
145      * @throws InvalidPolicies a list of indices identifying the
146      * position in the input policies list that are occupied by
147      * invalid policies
148      *
149      * @throws BAD_PARAM if the sequence contains two or more Policy
150      * objects with the same PolicyType value, the operation raises
151      * the standard sytem exception BAD_PARAM with standard minor code
152      * 30.
153      */

154
155     public synchronized void set_policy_overrides(org.omg.CORBA.Policy JavaDoc[] policies,
156                                                   org.omg.CORBA.SetOverrideType JavaDoc set_add)
157         throws org.omg.CORBA.InvalidPolicies
158     {
159         if (policies == null)
160             throw new IllegalArgumentException JavaDoc("Argument may not be null");
161
162         HashMap newPolicies = new HashMap();
163         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
164
165         // check that the policies argument does not contain multiple
166
// policies of the same type while (copying the list of policies)
167
// and does not override policies that ccannot be overriden
168
for (int i = 0; i < policies.length; i++ )
169         {
170
171             if (!PolicyUtil.isInvocationPolicy( policies[i].policy_type() ))
172             {
173                 throw new org.omg.CORBA.NO_PERMISSION JavaDoc("Not an invocation policy, type " +
174                                                       policies[i].policy_type() );
175             }
176             // else:
177
Integer JavaDoc key = new Integer JavaDoc( policies[i].policy_type());
178             if ( newPolicies.put( key, policies[i] ) != null )
179             {
180                 throw new org.omg.CORBA.BAD_PARAM JavaDoc( "Multiple policies of type " +
181                                                    policies[i].policy_type(),
182                                                    30,
183                                                    org.omg.CORBA.CompletionStatus.COMPLETED_NO);
184             }
185             sb.append(" " + policies[i].policy_type() );
186         }
187         
188
189         if (set_add == org.omg.CORBA.SetOverrideType.SET_OVERRIDE )
190         {
191             PolicyUtil.checkValidity(newPolicies);
192             if (logger.isDebugEnabled())
193                 logger.debug("SET_OVERRIDE, types: " + sb.toString());
194             policy_overrides = newPolicies;
195         }
196         else if (set_add == org.omg.CORBA.SetOverrideType.ADD_OVERRIDE )
197         {
198             // adds policies (and replaces any existing policies)
199
HashMap test = new HashMap();
200             test.putAll( newPolicies );
201             PolicyUtil.checkValidity(test);
202
203             if (logger.isDebugEnabled())
204                 logger.debug("ADD_OVERRIDE, types: " + sb.toString());
205             policy_overrides = test;
206         }
207
208     }
209
210 }
211
Popular Tags