001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es;
030:
031: import com.caucho.server.util.CauchoSystem;
032:
033: import java.util.Hashtable;
034:
035: /**
036: * JavaScript object representing a Java package or class.
037: */
038: class ESPackage extends ESBase {
039: ESString name;
040: Class cl;
041: Hashtable cache = new Hashtable();
042:
043: /**
044: * Creates a new package object with the given first segment.
045: *
046: * @param name the first segment of a Java package/class.
047: */
048: private ESPackage(String name) {
049: this .className = "Package";
050: this .prototype = esBase;
051:
052: this .name = ESString.create(name);
053: }
054:
055: /**
056: * Creates an empty package object.
057: */
058: static ESPackage create() {
059: return new ESPackage("");
060: }
061:
062: public ESBase getProperty(ESString name) throws Throwable {
063: ESBase value = null;
064:
065: if (name == null)
066: return this ;
067:
068: value = (ESBase) cache.get(name);
069:
070: if (value != null)
071: return value;
072:
073: String subname;
074: if (this .name.equals(ESString.NULL))
075: subname = name.toString();
076: else
077: subname = this .name.toString() + "." + name.toString();
078:
079: try {
080: Global global = Global.getGlobalProto();
081: ClassLoader loader = global.getParentLoader();
082:
083: Class cl = CauchoSystem.loadClass(subname, false, loader);
084:
085: value = global.classWrap(cl);
086: } catch (ClassNotFoundException e) {
087: value = new ESPackage(subname);
088: }
089:
090: cache.put(name, value);
091:
092: return value;
093: }
094:
095: public ESString toStr() {
096: return new ESString("[Package " + name + "]");
097: }
098:
099: public double toNum() {
100: return 0.0 / 0.0;
101: }
102:
103: /**
104: * Converts the package to a java object. For classes, returns the class.
105: * For a package, returns null.
106: */
107: public Object toJavaObject() {
108: return null;
109: }
110:
111: ESBase create(String string) {
112: try {
113: return getProperty(ESString.create(string));
114: } catch (Throwable e) {
115: return ESBase.esNull;
116: }
117: }
118: }
|