001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.ext.dbobj;
066:
067: import com.jcorporate.expresso.core.db.DBConnection;
068: import com.jcorporate.expresso.core.db.DBException;
069: import com.jcorporate.expresso.core.dbobj.DBObject;
070: import com.jcorporate.expresso.core.dbobj.RequestContext;
071: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
072: import org.apache.oro.util.CacheLRU;
073:
074: /**
075: * Table for countries that are not permitted access to 'restricted' downloads
076: * <p>Usually this pertains to Cryptographic source code that cannot be downloaded
077: * from the U.S. to Cuba for example.</p>
078: *
079: * @author Michael Rimov
080: */
081: public class RestrictedCountries extends SecuredDBObject {
082: static public final String FLD_RESTRICTID = "RestrictedID";
083: static public final String FLD_DOMAINID = "DomainID";
084:
085: /**
086: * Lookup Cache to prevent having to do reverse-name lookups all the time.
087: * for the same user
088: */
089: static protected CacheLRU lookupCache = new CacheLRU(20);
090:
091: /**
092: * Constructor
093: *
094: * @throws DBException if there's an initialization problem
095: */
096: public RestrictedCountries() throws DBException {
097: super ();
098: } /* User() */
099:
100: /**
101: * Constructor
102: *
103: * @param myConnection the DBConnection to assign to this DBObject
104: * @throws DBException if there's an initialization problem
105: */
106: public RestrictedCountries(DBConnection myConnection)
107: throws DBException {
108: super (myConnection);
109: } /* User(String) */
110:
111: /**
112: * Use over (String) constructor. Initializes the object in the context
113: * of the user who's uid belongs to the parameter.
114: *
115: * @param uid the Uid of the user context
116: * @throws DBException if there's an initialization problem
117: */
118: public RestrictedCountries(int uid) throws DBException {
119: super (uid);
120: }
121:
122: /**
123: * For using DBObjects within Controllers. Initializes based upon the current
124: * user and the requested db. [Of course this can be modified later]
125: *
126: * @param request - The controller request handed to you by the framework.
127: * @throws DBException if there's an initialization problem
128: */
129: public RestrictedCountries(RequestContext request)
130: throws DBException {
131: super (request);
132: }
133:
134: /**
135: * This function populates the table with the current listing of ISO country codes
136: *
137: * @throws DBException if an error occurs while populating the table.
138: * @see com.jcorporate.expresso.core.dbobj.DBObject#populateDefaultValues
139: */
140: public synchronized void populateDefaultValues() throws DBException {
141: int i;
142: int len = restrictedIDs.length;
143:
144: for (i = 0; i < len; i++) {
145: this .clear();
146: this .setField(FLD_DOMAINID, restrictedIDs[i]);
147:
148: if (!this .find()) {
149: this .add();
150: }
151: }
152: } /* populateDefaultValues() */
153:
154: /**
155: * Useful method for unit testing to make sure that everything got added during
156: * setup as expected.
157: *
158: * @return the length o
159: */
160: public int getExpectedDefaultPopulation() {
161: return restrictedIDs.length;
162: }
163:
164: /**
165: * @return an instantiated DBObject
166: * @throws DBException if an error occurs while populating the table.
167: * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject#getThisDBObj
168: */
169: public synchronized DBObject getThisDBObj() throws DBException {
170: return new RestrictedCountries();
171: } /* getThisDBObj() */
172:
173: /**
174: * @throws DBException
175: */
176: protected void setupFields() throws DBException {
177: setTargetTable("RESTRICTCNTRY");
178: setDescription("Reverse Domain Lookup Descriptions");
179: setCharset("ISO-8859-1");
180: addField(FLD_RESTRICTID, "auto-inc", 0, false,
181: "Restricted Country ID");
182: addField(FLD_DOMAINID, "varchar", 20, false,
183: "Restricted Domain ID");
184: addKey(FLD_RESTRICTID);
185: setLookupObject(FLD_DOMAINID,
186: "com.jcorporate.expresso.ext.dbobj.ReverseLookupDomains");
187: setMultiValued(FLD_DOMAINID);
188: } /* setupFields() */
189:
190: int[] restrictedIDs = {};
191:
192: /**
193: * Determines if the given IP adderss is a 'restricted' source. 127.0.0.1
194: * is always considered unrestricted.
195: *
196: * @param ipAddress the ipaddress as a string
197: * @return true if this ip address maps to a domain that's listed in the restricted
198: * table.
199: * @throws DBException upon error
200: */
201: public boolean isRestricted(String ipAddress) throws DBException {
202:
203: //
204: //Localhost is always unresticted
205: //
206: if (ipAddress.equals("127.0.0.1")) {
207: return false;
208: }
209:
210: Boolean cachedResult = this .getCachedLookup(ipAddress);
211:
212: if (cachedResult != null) {
213: return cachedResult.booleanValue();
214: }
215:
216: ReverseLookupDomains rld = new ReverseLookupDomains(this
217: .getRequestingUid());
218: rld.setDataContext(this .getDataContext());
219:
220: String domainID = rld.getDomainId(ipAddress);
221: RestrictedCountries rc = new RestrictedCountries(this
222: .getRequestingUid());
223: rc.setDataContext(this .getDataContext());
224: rc.setField(FLD_DOMAINID, domainID);
225:
226: if (rc.find() == true) {
227: cachedResult = Boolean.TRUE;
228: } else {
229: cachedResult = Boolean.FALSE;
230: }
231:
232: setCachedLookup(ipAddress, cachedResult);
233:
234: return cachedResult.booleanValue();
235: }
236:
237: /**
238: * Threadsafe cache dealing... check to see if the ip address is already
239: * in the listing
240: *
241: * @param ipAddress the ipAddress to check for cached values.
242: * @return java.lang.Boolean
243: */
244: protected synchronized Boolean getCachedLookup(String ipAddress) {
245: return (Boolean) lookupCache.getElement(ipAddress);
246: }
247:
248: /**
249: * Threadsafe cache dealing, sets the lookup ip address as the key, and the
250: * lookup results in the cache system.
251: *
252: * @param ipAddress the ipAddress to check for cached values.
253: * @param newValue java.lang.Boolean result to cache.
254: */
255: protected synchronized void setCachedLookup(String ipAddress,
256: Boolean newValue) {
257: lookupCache.addElement(ipAddress, newValue);
258: }
259: }
|