📄 abstractobjectcontainer.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Core License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-2000 by SMB GmbH. All rights reserved.//// $Id: AbstractObjectContainer.java,v 1.20 2000/10/28 16:55:16 daniela Exp $package org.ozoneDB.core;import java.io.*;import java.util.*;import java.lang.reflect.*;import org.ozoneDB.*;import org.ozoneDB.tools.OPP.*;import org.ozoneDB.DxLib.*;import org.ozoneDB.core.*;import org.ozoneDB.util.*;/** * @author <a href="http://www.softwarebuero.de/">SMB</a> * @version $Revision: 1.20 $Date: 2000/10/28 16:55:16 $ */public abstract class AbstractObjectContainer implements ObjectContainer { private static transient DxMap methodTable = new DxHashMap( 128 ); private static transient DxMap classTable = new DxHashMap( 128 ); private transient OzoneProxy theProxy; protected int state; public int state() { return state; } public void raiseState( int newState ) { state = newState > state ? newState : state; } public void clearState() { state = STATE_CLEAN; } public OzoneInterface database() { return Env.currentEnv().database; } public OzoneProxy ozoneProxy() { try { if (theProxy == null) { String implName = targetClass().getName(); String proxyName; if (implName.endsWith( ObjectContainer.IMPLNAME_POSTFIX )) { proxyName = implName.substring( 0, implName.length() - 5 ); } else { proxyName = implName + PROXYNAME_POSTFIX; } MethodKey key = new MethodKey( proxyName, "_ctor_", "(default)" ); Constructor ctor = (Constructor)methodTable.elementForKey( key ); if (ctor == null) { Class cl = Env.currentEnv().classManager.classForName( proxyName ); Class[] argTypes = {ObjectID.class, OzoneInterface.class}; ctor = cl.getConstructor( argTypes ); methodTable.addForKey( ctor, key ); } //System.out.println ("creating proxy: " + cl.getName()); Object[] args = {id(), database()}; theProxy = (OzoneProxy)ctor.newInstance( args ); // Env.currentEnv().logWriter.newEntry (this, "proxy added to cache - " + theProxy, LogWriter.DEBUG); } return theProxy; } catch (Exception e) { Env.currentEnv().logWriter.newEntry( this, "ozoneProxy(): unable to create proper proxy object.", e, LogWriter.WARN ); throw new RuntimeException( e.toString() ); } } public OzoneCompatible targetClone() throws Exception { ByteArrayOutputStream bout = new ByteArrayOutputStream( 2048 ); ObjectOutputStream out = new ObjectOutputStream( bout ); out.writeObject( target() ); out.close(); ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( bout.toByteArray() ) ); Object targetClone = (OzoneCompatible)in.readObject(); in.close(); return (OzoneCompatible)targetClone; } /** * Search the method with the specified name and signature. Once * a method has been invoked it is stored in a global cache that holds * the method objects of all database classes. * * * @param methodName * @param sig * @param args */ protected final Method methodFor( Env env, Object obj, String methodName, String sig, Object[] args ) throws Exception { MethodKey key = new MethodKey( targetClass().getName(), methodName, sig ); Method method = (Method)methodTable.elementForKey( key ); if (method == null) { Class[] classes; StringTokenizer st = new StringTokenizer( sig, OPP.SIGNATURE_DELIMITER ); classes = new Class[args.length]; for (int i = 0; st.hasMoreTokens(); ++i) { classes[i] = env.classManager.classForName( st.nextToken() ); } method = obj.getClass().getMethod( methodName, classes ); methodTable.addForKey( method, key ); env.logWriter.newEntry( this, "method added to cache - " + key, LogWriter.DEBUG ); } return method; } /** * Search the constructor with the specified signature. * Once a constructor has been invoked it is stored in a global cache that * holds the method and constructor objects of all database classes. * * * @param env * @param obj * @param sig */ protected Constructor constructorFor( Env env, Class cl, String sig ) throws Exception { MethodKey key = new MethodKey( cl.getName(), "_ctor_", sig ); Constructor constructor = (Constructor)methodTable.elementForKey( key ); if (constructor == null) { Class[] classes; if (sig == null) { classes = new Class[0]; } else { StringTokenizer st = new StringTokenizer( sig, OPP.SIGNATURE_DELIMITER ); classes = new Class[st.countTokens()]; for (int i = 0; st.hasMoreTokens(); ++i) { classes[i] = env.classManager.classForName( st.nextToken() ); } } constructor = cl.getConstructor( classes ); methodTable.addForKey( constructor, key ); env.logWriter.newEntry( this, "constructor added to cache - " + key, LogWriter.DEBUG ); } return constructor; } public static void flushMethodCache() { methodTable = new DxHashMap( 128 ); } public Object invokeTarget( Env env, String methodName, String sig, Object[] args ) throws Exception { Method method = methodFor( env, target(), methodName, sig, args ); if (method == null) { throw new MethodNotFoundExc( methodName ); } try { return method.invoke( target(), args ); } catch (InvocationTargetException e) { throw e; } } public Object invokeTarget( Env env, int methodIndex, Object[] args ) throws Exception { Class cl = targetClass(); Method[] methods = (Method[])classTable.elementForKey( cl ); if (methods == null) { methods = OPPHelper.methodsOfClass( cl ); classTable.addForKey( methods, cl ); } Method method = methods[methodIndex]; if (method == null) { throw new MethodNotFoundExc( "Method index: " + methodIndex ); } if (env.logWriter.hasTarget( LogWriter.DEBUG3 )) { env.logWriter.newEntry( this, "invoke(): method=" + method.getName(), LogWriter.DEBUG3 ); } try { return method.invoke( target(), args ); } catch (InvocationTargetException e) { throw e; } } public void createTarget( Env env, Class cl, String sig, Object[] args ) throws Exception { OzoneCompatible result; if (sig != null) { Constructor ctor = constructorFor( env, cl, sig ); result = (OzoneCompatible)ctor.newInstance( args ); } else { result = (OzoneCompatible)cl.newInstance(); } setTarget( result ); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -