KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > xml > dbobj > UserAgent


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.xml.dbobj;
66
67 import com.jcorporate.expresso.core.db.DBException;
68 import com.jcorporate.expresso.core.dbobj.DBObject;
69 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
70 import com.jcorporate.expresso.core.dbobj.ValidValue;
71 import org.apache.commons.collections.LRUMap;
72 import org.apache.oro.text.regex.MalformedPatternException;
73 import org.apache.oro.text.regex.Pattern;
74 import org.apache.oro.text.regex.PatternCompiler;
75 import org.apache.oro.text.regex.PatternMatcher;
76 import org.apache.oro.text.regex.Perl5Compiler;
77 import org.apache.oro.text.regex.Perl5Matcher;
78
79 import java.util.Iterator JavaDoc;
80 import java.util.Map JavaDoc;
81 import java.util.Vector JavaDoc;
82
83
84 //import com.jcorporate.expresso.ext.regexp.RE;
85
//import com.jcorporate.expresso.ext.regexp.RESyntaxException;
86

87 /**
88  * Entries here define the other VM's (even on other servers) that are also using
89  * this database, and with whom cache synchronizations should be attempted. Note
90  * that for this to work, the db/context names on the remote servers must be the
91  * same as the db/context names here.
92  *
93  * @author Michael Nash
94  */

95 public class UserAgent
96         extends SecuredDBObject {
97     private static final String JavaDoc thisClass = UserAgent.class.getName() + ".";
98
99     /**
100      * Used to hold compiled regular expressions in an attempt to speed
101      * up pattern matching.
102      */

103     protected static Map regExpCache = null;
104
105     /**
106      * Used for regular expression compilation
107      */

108     protected static PatternCompiler compiler = new Perl5Compiler();
109
110     /**
111      * Used to match compiled patterns against strings
112      */

113     protected static PatternMatcher matcher = new Perl5Matcher();
114
115     /**
116      * Constructor
117      */

118     public UserAgent()
119             throws DBException {
120         super();
121     } /* UserAgent() */
122
123     /**
124      * Constructor that sets the 'owner' of this DBObject class.
125      *
126      * @param uid The User's uid
127      * @throws DBException upon construction error
128      */

129     public UserAgent(int uid)
130             throws DBException {
131         super(uid);
132     } /* UserAgent(String) */
133
134     /**
135      * Creates the object cache based upon the size of the UserAgent map.
136      *
137      * @param agentList the user agent dbobject.
138      */

139     private synchronized void createObjectCache(UserAgent agentList) {
140         try {
141             if (regExpCache == null) {
142                 int numMatches = agentList.count();
143
144                 //Let's limit the size of the cache in case it's really
145
//ridiculous
146
if (numMatches > 60) {
147                     numMatches = 60;
148                 }
149
150                 regExpCache = new LRUMap(numMatches);
151             }
152         } catch (DBException dbe) {
153             regExpCache = new LRUMap(60);
154         }
155     }
156
157     /**
158      * Get's the regular expression patter to match against.
159      *
160      * @param userAgentPattern the pattern of the UserAgent
161      * @return a precompiled regular expression pattern to match.
162      * @throws DBException if the mattern match is bad.
163      */

164     private synchronized Pattern getPattern(String JavaDoc userAgentPattern)
165             throws DBException {
166         Pattern p = (Pattern) regExpCache.get(userAgentPattern);
167
168         if (p == null) {
169             try {
170                 p = compiler.compile(userAgentPattern, Perl5Compiler.READ_ONLY_MASK);
171                 regExpCache.put(userAgentPattern, p);
172             } catch (MalformedPatternException mpe) {
173                 throw new DBException(thisClass + "getPattern(String)", mpe);
174             }
175         }
176
177         return p;
178     }
179
180     public Vector JavaDoc getValues()
181             throws DBException {
182         return getValuesDefault("UserAgent", "Descrip");
183     }
184
185     /**
186      * Figure out which UserAgent entry this User-Agent string from the browser
187      * should match & set this db object to the appropriate object.
188      *
189      * @param userAgentString the string of the user agent to match against
190      * @return true if we get a successful match.
191      * @throws DBException upon database communication error
192      */

193     public boolean getMatch(String JavaDoc userAgentString)
194             throws DBException {
195         UserAgent oneAgent = null;
196         UserAgent agentList = new UserAgent();
197         agentList.setDataContext(getDataContext());
198         createObjectCache(agentList);
199
200         for (Iterator JavaDoc i = agentList.searchAndRetrieveList("MatchSeq").iterator();
201              i.hasNext();) {
202             oneAgent = (UserAgent) i.next();
203
204             String JavaDoc uaPattern = oneAgent.getField("UAPattern");
205             Pattern compiledRegExp = getPattern(uaPattern);
206
207             boolean result;
208             synchronized (matcher) {
209                 result = matcher.matches(userAgentString, compiledRegExp);
210             }
211             if (result) { //wlo: changed 'uaPattern' to 'userAgentString'
212
setField("UserAgent", oneAgent.getField("UserAgent"));
213                 retrieve();
214
215                 return true;
216             }
217         }
218
219         return false;
220     } /* getMatch(String) */
221
222
223     /**
224      * Standard method to return a new UserAgent object
225      *
226      * @return DBObject A new CacheSync object
227      * @throws DBException If the new object cannot be created
228      */

229     public DBObject getThisDBObj()
230             throws DBException {
231         return (DBObject) new UserAgent();
232     } /* getThisDBObj() */
233
234
235     /**
236      * Override the method getValues to provide specific values for our
237      * multi-valued fields
238      *
239      * @param fieldName Fielname to retrieve values for
240      * @return Vector of ValidValue Value/description pairs for this field
241      * @throws DBException If the values cannot be retrieved
242      */

243     public synchronized Vector JavaDoc getValidValues(String JavaDoc fieldName)
244             throws DBException {
245         if (fieldName.equals("Protocol")) {
246             Vector JavaDoc myValues = new Vector JavaDoc(4);
247             myValues.addElement(new ValidValue("http", "HTTP"));
248             myValues.addElement(new ValidValue("jobqueue", "Via Job Queue"));
249
250             return myValues;
251         }
252
253         return super.getValidValues(fieldName);
254     } /* getValidValues(String) */
255
256
257     /**
258      * Define the table and fields for this object
259      *
260      * @throws DBException If the fields cannot be set up
261      */

262     public void setupFields()
263             throws DBException {
264         setTargetTable("USERAGENT");
265         setDescription("User-Agent Matching");
266         setCharset("ISO-8859-1");
267         addField("UserAgent", "auto-inc", 0, false, "User Agent Id");
268         addField("MatchSeq", "int", 0, false, "Match Sequence");
269         addField("Descrip", "varchar", 80, false, "Description");
270         addField("UAPattern", "text", 0, false, "Pattern to Match");
271         addKey("UserAgent");
272         setReadOnly("UserAgent");
273     } /* setupFields() */
274
275
276     public synchronized void populateDefaultValues()
277             throws DBException {
278         UserAgent defAgent = new UserAgent();
279         defAgent.setDataContext(getDataContext());
280         defAgent.setField("UAPattern", ".*");
281
282         if (!defAgent.find()) {
283             defAgent.setField("Descrip", "Default Agent");
284             defAgent.setField("MatchSeq", "9999");
285             defAgent.add();
286         }
287     } /* populateDefaultValues() */
288
289
290 } /* UserAgent */
291
292 /* UserAgent */
Popular Tags