BundleLocationCondition.java

00001 /*
00002  * $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 $
00003  * 
00004  * Copyright (c) OSGi Alliance (2005, 2006). All Rights Reserved.
00005  * 
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *      http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 package org.osgi.service.condpermadmin;
00020 
00021 import java.security.AccessController;
00022 import java.security.PrivilegedAction;
00023 import java.util.Hashtable;
00024 
00025 import org.osgi.framework.*;
00026 
00033 public class BundleLocationCondition {
00034         private static final String     CONDITION_TYPE  = "org.osgi.service.condpermadmin.BundleLocationCondition";
00035 
00050         static public Condition getCondition(final Bundle bundle, ConditionInfo info) {
00051                 if (!CONDITION_TYPE.equals(info.getType()))
00052                         throw new IllegalArgumentException(
00053                                         "ConditionInfo must be of type \"" + CONDITION_TYPE + "\"");
00054                 String[] args = info.getArgs();
00055                 if (args.length != 1)
00056                         throw new IllegalArgumentException("Illegal number of args: "
00057                                         + args.length);
00058                 String bundleLocation = (String) AccessController
00059                                 .doPrivileged(new PrivilegedAction() {
00060                                         public Object run() {
00061                                                 return bundle.getLocation();
00062                                         }
00063                                 });
00064                 Filter filter = null;
00065                 try {
00066                         filter = FrameworkUtil.createFilter("(location="
00067                                         + escapeLocation(args[0]) + ")");
00068                 }
00069                 catch (InvalidSyntaxException e) {
00070                         // this should never happen, but just incase
00071                         throw new RuntimeException("Invalid filter: " + e.getFilter());
00072                 }
00073                 Hashtable matchProps = new Hashtable(2);
00074                 matchProps.put("location", bundleLocation);
00075                 return filter.match(matchProps) ? Condition.TRUE : Condition.FALSE;
00076         }
00077 
00078         private BundleLocationCondition() {
00079                 // private constructor to prevent objects of this type
00080         }
00081 
00089         private static String escapeLocation(String value) {
00090                 boolean escaped = false;
00091                 int inlen = value.length();
00092                 int outlen = inlen << 1; /* inlen * 2 */
00093 
00094                 char[] output = new char[outlen];
00095                 value.getChars(0, inlen, output, inlen);
00096 
00097                 int cursor = 0;
00098                 for (int i = inlen; i < outlen; i++) {
00099                         char c = output[i];
00100                         switch (c) {
00101                                 case '\\' :
00102                                         if (i + 1 < outlen && output[i + 1] == '*')
00103                                                 break;
00104                                 case '(' :
00105                                 case ')' :
00106                                         output[cursor] = '\\';
00107                                         cursor++;
00108                                         escaped = true;
00109                                         break;
00110                         }
00111 
00112                         output[cursor] = c;
00113                         cursor++;
00114                 }
00115 
00116                 return escaped ? new String(output, 0, cursor) : value;
00117         }
00118 }

Generated on Mon Jan 11 21:19:13 2010 for OpenMobileIS by  doxygen 1.5.4