KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > ResinModule


1 /*
2  * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

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 JavaDoc;
45 import javax.management.ObjectName JavaDoc;
46 import javax.naming.Context JavaDoc;
47 import javax.naming.InitialContext JavaDoc;
48 import javax.naming.NamingException JavaDoc;
49 import javax.transaction.UserTransaction JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.io.UnsupportedEncodingException JavaDoc;
52 import java.util.Hashtable JavaDoc;
53 import java.util.IdentityHashMap JavaDoc;
54 import java.util.Map JavaDoc;
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   /**
74    * Converts a string into its binary representation, according to the
75    * given encoding, if given, or the script encoding if not given.
76    */

77   public static Value string_to_binary(Env env, String JavaDoc string,
78                                                 @Optional String JavaDoc 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 JavaDoc e) {
88
89       env.error(e);
90
91       return BooleanValue.FALSE;
92     }
93   }
94
95   /**
96    * Perform a jndi lookup to retrieve an object.
97    *
98    * @param name a fully qualified name "java:comp/env/foo",
99    * or a short-form "foo".
100
101    * @return the object, or null if it is not found.
102    */

103   public static Object JavaDoc jndi_lookup(String JavaDoc name)
104   {
105     return Jndi.lookup(name);
106   }
107
108
109   /**
110    * Returns the version of the Resin server software.
111    */

112   public static String JavaDoc resin_version()
113   {
114     return Version.FULL_VERSION;
115   }
116
117   /**
118    * Starts a new distributed transaction.
119    */

120   public static Value xa_begin()
121   {
122     try {
123       getUserTransaction().begin();
124
125       return NullValue.NULL;
126     } catch (Exception JavaDoc e) {
127       throw new QuercusModuleException(e);
128     }
129   }
130
131   /**
132    * Commits the current transaction.
133    */

134   public static Value xa_commit()
135   {
136     try {
137       getUserTransaction().commit();
138
139       return NullValue.NULL;
140     } catch (Exception JavaDoc e) {
141       throw new QuercusModuleException(e);
142     }
143   }
144
145   /**
146    * Complets the current transaction by rolling it back.
147    */

148   public static Value xa_rollback()
149   {
150     try {
151       getUserTransaction().rollback();
152
153       return NullValue.NULL;
154     } catch (Exception JavaDoc e) {
155       throw new QuercusModuleException(e);
156     }
157   }
158
159   /**
160    * Sets the rollback_only status for the current transaction.
161    */

162   public static Value xa_rollback_only(String JavaDoc msg)
163   {
164     try {
165       getUserTransaction().setRollbackOnly();
166
167       return NullValue.NULL;
168     } catch (Exception JavaDoc e) {
169       throw new QuercusModuleException(e);
170     }
171   }
172
173   /**
174    * Sets the timeout for the current distribued transaction.
175    */

176   public static Value xa_set_timeout(int timeoutSeconds)
177   {
178     try {
179       getUserTransaction().setTransactionTimeout(timeoutSeconds);
180
181       return NullValue.NULL;
182     } catch (Exception JavaDoc e) {
183       throw new QuercusModuleException(e);
184     }
185   }
186
187   /**
188    * Returns the JTA status code for the current transation.
189    */

190   public static int xa_status()
191   {
192     // XXX: should return a string
193
try {
194       return getUserTransaction().getStatus();
195     } catch (Exception JavaDoc e) {
196       throw new QuercusModuleException(e);
197     }
198   }
199
200   /**
201    * Returns the UserTransaction object.
202    */

203   private static UserTransaction JavaDoc getUserTransaction()
204   {
205     try {
206       // XXX: this could be cached, since it's a constant for the
207
// current environment
208

209       Context JavaDoc ic = new InitialContext JavaDoc();
210       
211       return ((UserTransaction JavaDoc) ic.lookup("java:comp/UserTransaction"));
212     } catch (NamingException JavaDoc e) {
213       throw new QuercusModuleException(e);
214     }
215   }
216
217   /**
218    * Explode an object name into an array with key value pairs that
219    * correspond to the keys and values in the object name.
220    * The domain is stored in the returned array under the key named ":".
221    */

222   public ArrayValue mbean_explode(String JavaDoc name)
223   {
224     try {
225       ObjectName JavaDoc objectName = new ObjectName JavaDoc(name);
226
227       ArrayValueImpl exploded = new ArrayValueImpl();
228
229       exploded.put(":domain:", objectName.getDomain());
230
231       Hashtable JavaDoc<String JavaDoc, String JavaDoc> entries = objectName.getKeyPropertyList();
232
233       for (Map.Entry JavaDoc<String JavaDoc, String JavaDoc> entry : entries.entrySet()) {
234     exploded.put(entry.getKey(), entry.getValue());
235       }
236
237       return exploded;
238     } catch (MalformedObjectNameException JavaDoc e) {
239       throw new QuercusModuleException(e);
240     }
241   }
242
243   /**
244    * Implode an array into an object name. The array contains key value pairs
245    * that become key vlaue pairs in the object name. The key with the name
246    * ":" becomes the domain of the object name.
247    */

248   public static String JavaDoc mbean_implode(@NotNull @ReadOnly ArrayValue exploded)
249   {
250     try {
251       if (exploded == null)
252     return null;
253
254       String JavaDoc 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 JavaDoc<String JavaDoc, String JavaDoc> entries = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
264
265       for (Map.Entry JavaDoc<Value, Value> entry : exploded.entrySet()) {
266     String JavaDoc key = entry.getKey().toString();
267     String JavaDoc value = entry.getValue().toString();
268
269     if (":domain:".equals(key))
270       continue;
271
272     entries.put(key, value);
273       }
274
275       ObjectName JavaDoc objectName;
276
277       if (entries.isEmpty())
278     objectName = new ObjectName JavaDoc(domain + ":" + "*");
279       else
280     objectName = new ObjectName JavaDoc(domain, entries);
281
282       return objectName.getCanonicalName();
283     } catch (MalformedObjectNameException JavaDoc e) {
284       throw new QuercusModuleException(e);
285     }
286   }
287
288   /**
289    * Prints a debug version of the variable
290    *
291    * @param env the quercus calling environment
292    * @param v the variable to print
293    * @return the escaped stringPhp
294    */

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 JavaDoc<Value,String JavaDoc>());
302
303       out.println();
304
305       out.close();
306
307       return NullValue.NULL;
308     } catch (IOException JavaDoc e) {
309       throw new QuercusModuleException(e);
310     }
311   }
312 }
313
Popular Tags