KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > osgi > service > condpermadmin > BundleLocationCondition


1 /*
2  * $Header: /cvshome/build/org.osgi.service.condpermadmin/src/org/osgi/service/condpermadmin/BundleLocationCondition.java,v 1.18 2006/06/16 16:31:37 hargrave Exp $
3  *
4  * Copyright (c) OSGi Alliance (2005, 2006). All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.osgi.service.condpermadmin;
20
21 import java.security.AccessController JavaDoc;
22 import java.security.PrivilegedAction JavaDoc;
23 import java.util.Hashtable JavaDoc;
24
25 import org.osgi.framework.*;
26
27 /**
28  * Condition to test if the location of a bundle matches a pattern. Pattern
29  * matching is done according to the filter string matching rules.
30  *
31  * @version $Revision: 1.18 $
32  */

33 public class BundleLocationCondition {
34     private static final String JavaDoc CONDITION_TYPE = "org.osgi.service.condpermadmin.BundleLocationCondition";
35
36     /**
37      * Constructs a condition that tries to match the passed Bundle's location
38      * to the location pattern.
39      *
40      * @param bundle The Bundle being evaluated.
41      * @param info The ConditionInfo to construct the condition for. The args of
42      * the ConditionInfo must be a single String which specifies the
43      * location pattern to match against the Bundle location. Matching is
44      * done according to the filter string matching rules. Any '*'
45      * characters in the location argument are used as wildcards when
46      * matching bundle locations unless they are escaped with a '\'
47      * character.
48      * @return Condition object for the requested condition.
49      */

50     static public Condition getCondition(final Bundle bundle, ConditionInfo info) {
51         if (!CONDITION_TYPE.equals(info.getType()))
52             throw new IllegalArgumentException JavaDoc(
53                     "ConditionInfo must be of type \"" + CONDITION_TYPE + "\"");
54         String JavaDoc[] args = info.getArgs();
55         if (args.length != 1)
56             throw new IllegalArgumentException JavaDoc("Illegal number of args: "
57                     + args.length);
58         String JavaDoc bundleLocation = (String JavaDoc) AccessController
59                 .doPrivileged(new PrivilegedAction JavaDoc() {
60                     public Object JavaDoc run() {
61                         return bundle.getLocation();
62                     }
63                 });
64         Filter filter = null;
65         try {
66             filter = FrameworkUtil.createFilter("(location="
67                     + escapeLocation(args[0]) + ")");
68         }
69         catch (InvalidSyntaxException e) {
70             // this should never happen, but just incase
71
throw new RuntimeException JavaDoc("Invalid filter: " + e.getFilter());
72         }
73         Hashtable JavaDoc matchProps = new Hashtable JavaDoc(2);
74         matchProps.put("location", bundleLocation);
75         return filter.match(matchProps) ? Condition.TRUE : Condition.FALSE;
76     }
77
78     private BundleLocationCondition() {
79         // private constructor to prevent objects of this type
80
}
81
82     /**
83      * Escape the value string such that '(', ')' and '\' are escaped. The '\'
84      * char is only escaped if it is not followed by a '*'.
85      *
86      * @param value unescaped value string.
87      * @return escaped value string.
88      */

89     private static String JavaDoc escapeLocation(String JavaDoc value) {
90         boolean escaped = false;
91         int inlen = value.length();
92         int outlen = inlen << 1; /* inlen * 2 */
93
94         char[] output = new char[outlen];
95         value.getChars(0, inlen, output, inlen);
96
97         int cursor = 0;
98         for (int i = inlen; i < outlen; i++) {
99             char c = output[i];
100             switch (c) {
101                 case '\\' :
102                     if (i + 1 < outlen && output[i + 1] == '*')
103                         break;
104                 case '(' :
105                 case ')' :
106                     output[cursor] = '\\';
107                     cursor++;
108                     escaped = true;
109                     break;
110             }
111
112             output[cursor] = c;
113             cursor++;
114         }
115
116         return escaped ? new String JavaDoc(output, 0, cursor) : value;
117     }
118 }
119
Popular Tags