1 28 29 30 package com.caucho.quercus.lib; 31 32 import com.caucho.Version; 33 import com.caucho.naming.Jndi; 34 import com.caucho.quercus.QuercusModuleException; 35 import com.caucho.quercus.annotation.NotNull; 36 import com.caucho.quercus.annotation.Optional; 37 import com.caucho.quercus.annotation.ReadOnly; 38 import com.caucho.quercus.env.*; 39 import com.caucho.quercus.module.AbstractQuercusModule; 40 import com.caucho.util.L10N; 41 import com.caucho.vfs.Vfs; 42 import com.caucho.vfs.WriteStream; 43 44 import javax.management.MalformedObjectNameException ; 45 import javax.management.ObjectName ; 46 import javax.naming.Context ; 47 import javax.naming.InitialContext ; 48 import javax.naming.NamingException ; 49 import javax.transaction.UserTransaction ; 50 import java.io.IOException ; 51 import java.io.UnsupportedEncodingException ; 52 import java.util.Hashtable ; 53 import java.util.IdentityHashMap ; 54 import java.util.Map ; 55 56 57 public class ResinModule 58 extends AbstractQuercusModule 59 { 60 private static final L10N L = new L10N(ResinModule.class); 61 62 public final static int XA_STATUS_ACTIVE = 0; 63 public final static int XA_STATUS_MARKED_ROLLBACK = 1; 64 public final static int XA_STATUS_PREPARED = 2; 65 public final static int XA_STATUS_COMMITTED = 3; 66 public final static int XA_STATUS_ROLLEDBACK = 4; 67 public final static int XA_STATUS_UNKNOWN = 5; 68 public final static int XA_STATUS_NO_TRANSACTION = 6; 69 public final static int XA_STATUS_PREPARING = 7; 70 public final static int XA_STATUS_COMMITTING = 8; 71 public final static int XA_STATUS_ROLLING_BACK = 9; 72 73 77 public static Value string_to_binary(Env env, String string, 78 @Optional String encoding) 79 { 80 if (encoding == null || encoding.length() == 0) 81 encoding = env.getScriptEncoding(); 82 83 try { 84 byte[] bytes = string.getBytes(encoding); 85 86 return new BinaryBuilderValue(bytes); 87 } catch (UnsupportedEncodingException e) { 88 89 env.error(e); 90 91 return BooleanValue.FALSE; 92 } 93 } 94 95 103 public static Object jndi_lookup(String name) 104 { 105 return Jndi.lookup(name); 106 } 107 108 109 112 public static String resin_version() 113 { 114 return Version.FULL_VERSION; 115 } 116 117 120 public static Value xa_begin() 121 { 122 try { 123 getUserTransaction().begin(); 124 125 return NullValue.NULL; 126 } catch (Exception e) { 127 throw new QuercusModuleException(e); 128 } 129 } 130 131 134 public static Value xa_commit() 135 { 136 try { 137 getUserTransaction().commit(); 138 139 return NullValue.NULL; 140 } catch (Exception e) { 141 throw new QuercusModuleException(e); 142 } 143 } 144 145 148 public static Value xa_rollback() 149 { 150 try { 151 getUserTransaction().rollback(); 152 153 return NullValue.NULL; 154 } catch (Exception e) { 155 throw new QuercusModuleException(e); 156 } 157 } 158 159 162 public static Value xa_rollback_only(String msg) 163 { 164 try { 165 getUserTransaction().setRollbackOnly(); 166 167 return NullValue.NULL; 168 } catch (Exception e) { 169 throw new QuercusModuleException(e); 170 } 171 } 172 173 176 public static Value xa_set_timeout(int timeoutSeconds) 177 { 178 try { 179 getUserTransaction().setTransactionTimeout(timeoutSeconds); 180 181 return NullValue.NULL; 182 } catch (Exception e) { 183 throw new QuercusModuleException(e); 184 } 185 } 186 187 190 public static int xa_status() 191 { 192 try { 194 return getUserTransaction().getStatus(); 195 } catch (Exception e) { 196 throw new QuercusModuleException(e); 197 } 198 } 199 200 203 private static UserTransaction getUserTransaction() 204 { 205 try { 206 209 Context ic = new InitialContext (); 210 211 return ((UserTransaction ) ic.lookup("java:comp/UserTransaction")); 212 } catch (NamingException e) { 213 throw new QuercusModuleException(e); 214 } 215 } 216 217 222 public ArrayValue mbean_explode(String name) 223 { 224 try { 225 ObjectName objectName = new ObjectName (name); 226 227 ArrayValueImpl exploded = new ArrayValueImpl(); 228 229 exploded.put(":domain:", objectName.getDomain()); 230 231 Hashtable <String , String > entries = objectName.getKeyPropertyList(); 232 233 for (Map.Entry <String , String > entry : entries.entrySet()) { 234 exploded.put(entry.getKey(), entry.getValue()); 235 } 236 237 return exploded; 238 } catch (MalformedObjectNameException e) { 239 throw new QuercusModuleException(e); 240 } 241 } 242 243 248 public static String mbean_implode(@NotNull @ReadOnly ArrayValue exploded) 249 { 250 try { 251 if (exploded == null) 252 return null; 253 254 String domain; 255 256 Value domainValue = exploded.get(StringValue.create(":domain:")); 257 258 if (domainValue.isNull()) 259 domain = "*"; 260 else 261 domain = domainValue.toString(); 262 263 Hashtable <String , String > entries = new Hashtable <String , String >(); 264 265 for (Map.Entry <Value, Value> entry : exploded.entrySet()) { 266 String key = entry.getKey().toString(); 267 String value = entry.getValue().toString(); 268 269 if (":domain:".equals(key)) 270 continue; 271 272 entries.put(key, value); 273 } 274 275 ObjectName objectName; 276 277 if (entries.isEmpty()) 278 objectName = new ObjectName (domain + ":" + "*"); 279 else 280 objectName = new ObjectName (domain, entries); 281 282 return objectName.getCanonicalName(); 283 } catch (MalformedObjectNameException e) { 284 throw new QuercusModuleException(e); 285 } 286 } 287 288 295 public static Value resin_var_dump(Env env, @ReadOnly Value v) 296 { 297 try { 298 WriteStream out = Vfs.openWrite("stdout:"); 299 300 if (v != null) 301 v.varDump(env, out, 0, new IdentityHashMap <Value,String >()); 302 303 out.println(); 304 305 out.close(); 306 307 return NullValue.NULL; 308 } catch (IOException e) { 309 throw new QuercusModuleException(e); 310 } 311 } 312 } 313 | Popular Tags |