KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > dbobj > RestrictedCountries


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.dbobj;
66
67 import com.jcorporate.expresso.core.db.DBConnection;
68 import com.jcorporate.expresso.core.db.DBException;
69 import com.jcorporate.expresso.core.dbobj.DBObject;
70 import com.jcorporate.expresso.core.dbobj.RequestContext;
71 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
72 import org.apache.oro.util.CacheLRU;
73
74
75 /**
76  * Table for countries that are not permitted access to 'restricted' downloads
77  * <p>Usually this pertains to Cryptographic source code that cannot be downloaded
78  * from the U.S. to Cuba for example.</p>
79  *
80  * @author Michael Rimov
81  */

82 public class RestrictedCountries
83         extends SecuredDBObject {
84     static public final String JavaDoc FLD_RESTRICTID = "RestrictedID";
85     static public final String JavaDoc FLD_DOMAINID = "DomainID";
86
87     /**
88      * Lookup Cache to prevent having to do reverse-name lookups all the time.
89      * for the same user
90      */

91     static protected CacheLRU lookupCache = new CacheLRU(20);
92
93     /**
94      * Constructor
95      *
96      * @throws DBException if there's an initialization problem
97      */

98     public RestrictedCountries()
99             throws DBException {
100         super();
101     } /* User() */
102
103     /**
104      * Constructor
105      *
106      * @param myConnection the DBConnection to assign to this DBObject
107      * @throws DBException if there's an initialization problem
108      */

109     public RestrictedCountries(DBConnection myConnection)
110             throws DBException {
111         super(myConnection);
112     } /* User(String) */
113
114     /**
115      * Use over (String) constructor. Initializes the object in the context
116      * of the user who's uid belongs to the parameter.
117      *
118      * @param uid the Uid of the user context
119      * @throws DBException if there's an initialization problem
120      */

121     public RestrictedCountries(int uid)
122             throws DBException {
123         super(uid);
124     }
125
126     /**
127      * For using DBObjects within Controllers. Initializes based upon the current
128      * user and the requested db. [Of course this can be modified later]
129      *
130      * @param request - The controller request handed to you by the framework.
131      * @throws DBException if there's an initialization problem
132      */

133     public RestrictedCountries(RequestContext request)
134             throws DBException {
135         super(request);
136     }
137
138     /**
139      * This function populates the table with the current listing of ISO country codes
140      *
141      * @throws DBException if an error occurs while populating the table.
142      * @see com.jcorporate.expresso.core.dbobj.DBObject#populateDefaultValues
143      */

144     public synchronized void populateDefaultValues()
145             throws DBException {
146         int i;
147         int len = restrictedIDs.length;
148
149         for (i = 0; i < len; i++) {
150             this.clear();
151             this.setField(FLD_DOMAINID, restrictedIDs[i]);
152
153             if (!this.find()) {
154                 this.add();
155             }
156         }
157     } /* populateDefaultValues() */
158
159
160     /**
161      * Useful method for unit testing to make sure that everything got added during
162      * setup as expected.
163      *
164      * @return the length o
165      */

166     public int getExpectedDefaultPopulation() {
167         return restrictedIDs.length;
168     }
169
170     /**
171      * @return an instantiated DBObject
172      * @throws DBException if an error occurs while populating the table.
173      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#getThisDBObj
174      */

175     public synchronized DBObject getThisDBObj()
176             throws DBException {
177         return new RestrictedCountries();
178     } /* getThisDBObj() */
179
180
181     /**
182      * @throws DBException
183      */

184     protected void setupFields()
185             throws DBException {
186         setTargetTable("RESTRICTCNTRY");
187         setDescription("Reverse Domain Lookup Descriptions");
188         setCharset("ISO-8859-1");
189         addField(FLD_RESTRICTID, "auto-inc", 0, false, "Restricted Country ID");
190         addField(FLD_DOMAINID, "varchar", 20, false, "Restricted Domain ID");
191         addKey(FLD_RESTRICTID);
192         setLookupObject(FLD_DOMAINID,
193                 "com.jcorporate.expresso.ext.dbobj.ReverseLookupDomains");
194         setMultiValued(FLD_DOMAINID);
195     } /* setupFields() */
196
197
198     int[] restrictedIDs = {};
199
200     /**
201      * Determines if the given IP adderss is a 'restricted' source. 127.0.0.1
202      * is always considered unrestricted.
203      *
204      * @param ipAddress the ipaddress as a string
205      * @return true if this ip address maps to a domain that's listed in the restricted
206      * table.
207      * @throws DBException upon error
208      */

209     public boolean isRestricted(String JavaDoc ipAddress)
210             throws DBException {
211
212         //
213
//Localhost is always unresticted
214
//
215
if (ipAddress.equals("127.0.0.1")) {
216             return false;
217         }
218
219         Boolean JavaDoc cachedResult = this.getCachedLookup(ipAddress);
220
221         if (cachedResult != null) {
222             return cachedResult.booleanValue();
223         }
224
225         ReverseLookupDomains rld = new ReverseLookupDomains(this.getRequestingUid());
226         rld.setDataContext(this.getDataContext());
227
228         String JavaDoc domainID = rld.getDomainId(ipAddress);
229         RestrictedCountries rc = new RestrictedCountries(this.getRequestingUid());
230         rc.setDataContext(this.getDataContext());
231         rc.setField(FLD_DOMAINID, domainID);
232
233         if (rc.find() == true) {
234             cachedResult = Boolean.TRUE;
235         } else {
236             cachedResult = Boolean.FALSE;
237         }
238
239         setCachedLookup(ipAddress, cachedResult);
240
241         return cachedResult.booleanValue();
242     }
243
244     /**
245      * Threadsafe cache dealing... check to see if the ip address is already
246      * in the listing
247      *
248      * @param ipAddress the ipAddress to check for cached values.
249      * @return java.lang.Boolean
250      */

251     protected synchronized Boolean JavaDoc getCachedLookup(String JavaDoc ipAddress) {
252         return (Boolean JavaDoc) lookupCache.getElement(ipAddress);
253     }
254
255     /**
256      * Threadsafe cache dealing, sets the lookup ip address as the key, and the
257      * lookup results in the cache system.
258      *
259      * @param ipAddress the ipAddress to check for cached values.
260      * @param newValue java.lang.Boolean result to cache.
261      */

262     protected synchronized void setCachedLookup(String JavaDoc ipAddress,
263                                                 Boolean JavaDoc newValue) {
264         lookupCache.addElement(ipAddress, newValue);
265     }
266 }
Popular Tags