⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 externaldatabase.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public static ExternalDatabase openDatabase(String _url, String _username, String _passwd) throws Exception {

        Hashtable props = createProps(_url);
        props.put(PROP_USER, _username);
        props.put(PROP_PASSWD, _passwd);

        if (_url.startsWith("ozonedb:remote")) {
            RemoteDatabase db = new RemoteDatabase();
            db.open(props);
            return db;
        } else if (_url.startsWith("ozonedb:local")) {
            LocalDatabase db = new LocalDatabase();
            db.open(props);
            return db;
        } else {
            throw new MalformedURLException(_url);
        }
    }


    /**
     * @param _url The URL of the database (ozonedb:remote://host:port?user=<name>&password=<pwd> or
     * ozonedb:local:///datadir?user=<name>&password=<pwd>)
     */
    public static ExternalDatabase openDatabase(String _url) throws Exception {

        Hashtable props = createProps(_url);

        if (_url.startsWith("ozonedb:remote")) {
            RemoteDatabase db = new RemoteDatabase();
            db.open(props);
            return db;
        } else if (_url.startsWith("ozonedb:local")) {
            LocalDatabase db = new LocalDatabase();
            db.open(props);
            return db;
        } else {
            throw new MalformedURLException(_url);
        }
    }


    /**
     * @param _url The URL of the database (ozonedb:remote://host:port or
     * ozonedb:local://datadir)
     */
    protected static Hashtable createProps(String _url) throws URISyntaxException, UnknownHostException {
        Hashtable props = new Hashtable();

        URI uri = new URI(_url);
        String protocol = uri.getScheme();
        if (!protocol.equals("ozonedb")) {
            throw new URISyntaxException(_url, "unknown protocol: " + protocol);
        }

        URI subUri = new URI(uri.getSchemeSpecificPart());

        String protocol2 = subUri.getScheme();
        if (protocol2.equals("local")) {
            String filename = subUri.getPath();
            props.put(PROP_DIR, filename);
        } else if (protocol2.equals("remote")) {
            String hostname = subUri.getHost();
            if (hostname == null) throw new UnknownHostException("host is null");
            int port = subUri.getPort();
            props.put(PROP_HOST, hostname);
            props.put(PROP_PORT, new Integer(port));
        } else {
            throw new URISyntaxException(_url, "protocol: " + protocol2);
        }

        String query = subUri.getQuery();
        // Note: could have use the (now deprecated) javax.servlet.http.HttpUtils.parseQueryString(),
        // but that would come at the cost of ozone being j2ee dependend...
        String user = null;
        String password = null;
        if (query != null) {
            for(StringTokenizer amp = new StringTokenizer(query, "&"); amp.hasMoreElements(); ) {
                String keyWithValue = amp.nextToken();
                StringTokenizer eq = new StringTokenizer(keyWithValue, "=");
                if (eq.countTokens() != 2) throw new URISyntaxException(_url, "could not parse: " + keyWithValue);
                String key = eq.nextToken();
                if (key.equals("user")) {
                    if (user == null) {
                        user = eq.nextToken();
                    } else throw new URISyntaxException(_url, "duplicate occurence of option " + key);
                } else if (key.equals("password")) {
                    if (password == null) {
                        password = eq.nextToken();
                    } else throw new URISyntaxException(_url, "duplicate occurence of option " + key);
                } else throw new URISyntaxException(_url, "unknown option " + key);
            }
        }

        if (user == null) {
            user = System.getProperty("org.ozoneDB.user.name");
            if (user == null) {
                user = System.getProperty("user.name");
            }
            if (user == null) {
                user="noname";
            }
        }
        if (password == null) {
            password = "";
        }
        props.put(PROP_USER, user);
        props.put(PROP_PASSWD, password);

        return props;
    }

    /**
     * Close this database.
     */
    public synchronized void close() throws Exception {
        if (isOpen()) {
            databases.remove(this);

            // close all current database connections.
            try {
                apool.addAll(upool);

                DxIterator it = apool.iterator();
                DbClient connection;
                while ((connection = (DbClient) it.next()) != null) {
                    try {
                        connection.send(new DbCloseConn());
                    }catch(java.io.IOException e){
                        //lost connection, so couldn't send
                        //but we're trying to close, so that's OK
                    }
                    Thread.sleep(1000);
                    try {
                        connection.close();
                    }catch(java.io.IOException e){
                        //lost connection, so couldn't send
                        //but we're trying to close, so that's OK
                    }
                    it.removeObject();
                }
            } finally {
                apool = null;
                upool = null;
                txTable = null;
            }
        }
    }


    protected void finalize() throws Throwable {
        close();
    }


    public void reloadClasses() throws Exception {
        Integer result = (Integer) sendCommand(new DbReloadClasses(), true);
    }


    public OzoneProxy createObject( String className, int access, String name, String sig, Object[] args ) throws RuntimeException, OzoneObjectException {
        try {
            OzoneProxy proxy = (OzoneProxy) sendCommand( new DbCreateObj( className, access, name, sig, args ), true );

            if (org.ozoneDB.core.Env.selfCheck) {
                if (proxy==null) { // The proxy should never be null.
                    throw new Error("Found during createObject(\""+className+"\","+access+","+name+",\""+sig+"\"): returned proxy is "+proxy);
                }
            }

            return proxy;
        } catch (OzoneObjectException e) {
            throw e;
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            // only supported from JDK1.4 on
//          throw new RuntimeException("Caught during createObject(\""+className+"\","+access+","+name+",\""+sig+"\")",e);
            throw new RuntimeException("Caught during createObject(\""+className+"\","+access+","+name+",\""+sig+"\"): "+e);
        }
    }


    public void deleteObject( OzoneRemote obj ) throws RuntimeException,OzoneObjectException {
        try {
            sendCommand( new DbDeleteObj( (OzoneProxy)obj ), true );
        } catch (OzoneObjectException e) {
            throw e;
        } catch (Exception e) {
            // only supported from JDK1.4 on
            throw new RuntimeException("Caught during deleteObject()",e);
            //throw new RuntimeException("Caught during deleteObject(): "+e);
        }
    }


    public OzoneProxy copyObject(OzoneRemote obj) throws Exception {
        return (OzoneProxy) sendCommand(new DbCopyObj((OzoneProxy) obj), true);
    }


    public void nameObject(OzoneRemote obj, String name) throws Exception {
        sendCommand(new DbNameObj((OzoneProxy) obj, name), true);
    }


    public OzoneProxy objectForName(String name) throws Exception {
        return (OzoneProxy) sendCommand(new DbObjForName(name), true);
    }

    public String[] objectNames() throws Exception {
        //todo: maybe we should filter out Admin (AdminImpl.OZONE_DOCUMENT_FACTORY)?
        DxSet nameSet = (DxSet)sendCommand(new DbObjectNames(), true);
        Object[] keys = nameSet.toArray();
        String[] names = new String[keys.length];
        for (int i = 0; i < keys.length; i++) {
            names[i] = (String) keys[i];
        }
        return names;
    }

    public OzoneProxy objectForHandle(String handle) throws Exception {
        return (OzoneProxy) sendCommand(new DbObjForHandle(handle), true);
    }


    public Object invoke(OzoneProxy rObj, String methodName, String sig, Object[] args, int lockLevel)
            throws Exception {
        // Kommando verschicken
        Object result = sendCommand(new DbInvoke(rObj, methodName, sig, args, lockLevel), true);
        return result;
    }


    public Object invoke(OzoneProxy rObj, int methodIndex, Object[] args, int lockLevel) throws Exception {
        // Kommando verschicken
        Object result = sendCommand(new DbInvoke(rObj, methodIndex, args, lockLevel), true);
        return result;
    }


    public OzoneCompatible fetch(OzoneProxy rObj, int lockLevel) throws Exception,org.ozoneDB.ObjectNotFoundException, java.io.IOException, java.lang.ClassNotFoundException, org.ozoneDB.TransactionException, org.ozoneDB.core.TransactionError {
        return null;
    }


    public Node xmlForObject(OzoneRemote rObj, Document domFactory) throws Exception {
        byte[] bytes = (byte[]) sendCommand(new DbXMLForObj((OzoneProxy) rObj), true);

        SAXChunkConsumer consumer = new SAXChunkConsumer(domFactory, null);
        consumer.processChunk(bytes);

        return consumer.getResultNode();
    }


    public void xmlForObject(OzoneRemote rObj, ContentHandler ch) throws Exception {
        byte[] bytes = (byte[]) sendCommand(new DbXMLForObj((OzoneProxy) rObj), true);

        SAXChunkConsumer consumer = new SAXChunkConsumer(ch);
        consumer.processChunk(bytes);
    }


    /**
     * Return the administration object for this database.
     *
     * @return The admin object for this database;
     */
    public Admin admin() throws Exception {
        Admin admin = (Admin) objectForName(AdminImpl.OBJECT_NAME);
        return admin;
    }

    /**
    	Internal method. This method is called by {@link OzoneProxy}s when they are dying (during finalize()). This
    	is required, as the database may track the references the database client has to objects within the database
    	in order to properly support garbage collection. If this method is called from anyone else than from the
    	{@link OzoneProxy}.finalize()-Method, data loss may occur!

    	@param proxy the OzoneProxy object which is dying. It may call this method exaclty once.
    */
    public void notifyProxyDeath(OzoneProxy proxy) {
        try {
            sendCommand(new DbProxyDeath(proxy),false);
        } catch (Exception e) {
            // only supported from JDK1.4 on
//          throw new RuntimeException("Caught during notifyProxyDeath()",e);
            throw new RuntimeException("Caught during notifyProxyDeath(): "+e);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -