KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc6 > session > Util


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22
23 package org.jboss.web.tomcat.tc6.session;
24
25 import java.io.Serializable JavaDoc;
26 import java.lang.reflect.Field JavaDoc;
27 import java.lang.reflect.Modifier JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import org.jboss.aop.Advised;
35
36 /**
37  * Utility methods related to JBoss distributed sessions.
38  *
39  * @author Brian Stansberry
40  * @version $Revision: 56542 $
41  */

42 public class Util
43 {
44    // Types that are considered "primitive".
45
private static final Set JavaDoc immediates =
46       new HashSet JavaDoc(Arrays.asList(new Object JavaDoc[]{
47          String JavaDoc.class,
48          Boolean JavaDoc.class,
49          Double JavaDoc.class,
50          Float JavaDoc.class,
51          Integer JavaDoc.class,
52          Long JavaDoc.class,
53          Short JavaDoc.class,
54          Character JavaDoc.class,
55          Boolean.TYPE,
56          Double.TYPE,
57          Float.TYPE,
58          Integer.TYPE,
59          Long.TYPE,
60          Short.TYPE,
61          Character.TYPE,
62          Class JavaDoc.class}));
63
64    /**
65     * Returns a session id with any trailing jvmRoute removed.
66     *
67     * @param sessionId the raw session id
68     *
69     * @return <code>sessionId</code> with the final '.' and any
70     * characters thereafter removed.
71     */

72    public static String JavaDoc getRealId(String JavaDoc sessionId)
73    {
74       int index = sessionId.lastIndexOf(".");
75       if (index > 0)
76       {
77          return sessionId.substring(0, index);
78       }
79       else
80       {
81          return sessionId;
82       }
83    }
84
85    /**
86     * Checks whether the given object is usable for FIELD granularity
87     * replication.
88     *
89     * @param pojo the pojo
90     * @return <code>true</code> if the attribute type is acceptable,
91     * <code>false</code> otherwise
92     */

93    public static boolean checkPojoType(Object JavaDoc pojo)
94    {
95       return ( (pojo instanceof Serializable JavaDoc)
96               || (pojo instanceof Collection JavaDoc)
97               || (pojo instanceof Map JavaDoc)
98               || (pojo instanceof Advised)
99               || (immediates.contains(pojo.getClass())));
100    }
101    
102    public static Set JavaDoc parseComplexFields(Class JavaDoc clazz)
103    {
104       Set JavaDoc result = new HashSet JavaDoc();
105       
106       while (clazz != null)
107       {
108          Field JavaDoc[] fields = clazz.getDeclaredFields();
109          for (int i = 0; i < fields.length; i++)
110          {
111             if (!immediates.contains(fields[i].getType())
112                   && isReplicatable(fields[i]))
113             {
114                result.add(fields[i].getName());
115             }
116          }
117          
118          clazz = clazz.getSuperclass();
119       }
120       return result;
121    }
122
123    /**
124     * Returns false if the given field is static, transient or final.
125     *
126     * @param f the field
127     * @return
128     */

129    public static boolean isReplicatable(Field JavaDoc f) {
130       int mods = f.getModifiers();
131       /**
132        * The following modifiers are ignored in the cache, i.e., they will not be stored in the cache.
133        * Whenever, user trying to access these fields, it will be accessed from the in-memory version.
134        */

135       if (Modifier.isStatic(mods)
136             || Modifier.isTransient(mods)
137             || Modifier.isFinal(mods)) {
138          return false;
139       }
140       return true;
141    }
142
143    /**
144     * Prevent instantiation.
145     */

146    private Util() {}
147
148 }
149
Popular Tags