1 18 19 package org.osgi.service.condpermadmin; 20 21 import java.security.AccessController ; 22 import java.security.PrivilegedAction ; 23 import java.util.Hashtable ; 24 25 import org.osgi.framework.*; 26 27 33 public class BundleLocationCondition { 34 private static final String CONDITION_TYPE = "org.osgi.service.condpermadmin.BundleLocationCondition"; 35 36 50 static public Condition getCondition(final Bundle bundle, ConditionInfo info) { 51 if (!CONDITION_TYPE.equals(info.getType())) 52 throw new IllegalArgumentException ( 53 "ConditionInfo must be of type \"" + CONDITION_TYPE + "\""); 54 String [] args = info.getArgs(); 55 if (args.length != 1) 56 throw new IllegalArgumentException ("Illegal number of args: " 57 + args.length); 58 String bundleLocation = (String ) AccessController 59 .doPrivileged(new PrivilegedAction () { 60 public Object 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 throw new RuntimeException ("Invalid filter: " + e.getFilter()); 72 } 73 Hashtable matchProps = new Hashtable (2); 74 matchProps.put("location", bundleLocation); 75 return filter.match(matchProps) ? Condition.TRUE : Condition.FALSE; 76 } 77 78 private BundleLocationCondition() { 79 } 81 82 89 private static String escapeLocation(String value) { 90 boolean escaped = false; 91 int inlen = value.length(); 92 int outlen = inlen << 1; 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 (output, 0, cursor) : value; 117 } 118 } 119 | Popular Tags |