servicecontext.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 742 行 · 第 1/2 页

JAVA
742
字号
            tmpHashMap = new HashMap(tmpMap);
        }

        ObjectStateUtils.writeHashMap(out, tmpHashMap, "ServiceContext.properties");

        //---------------------------------------------------------
        // AxisService
        //---------------------------------------------------------

        String axisServMarker = "ServiceContext.metaAxisService";
        ObjectStateUtils.writeString(out, axisServMarker, axisServMarker);

        if (axisService == null) {
            out.writeBoolean(ObjectStateUtils.EMPTY_OBJECT);
        } else {
            out.writeBoolean(ObjectStateUtils.ACTIVE_OBJECT);
            metaAxisService =
                    new MetaDataEntry(axisService.getClass().getName(), axisService.getName());
            ObjectStateUtils.writeObject(out, metaAxisService, "ServiceContext.metaAxisService");
        }

        //---------------------------------------------------------
        // parent 
        //---------------------------------------------------------
        // ServiceGroupContext serviceGroupContext;

        ServiceGroupContext myParent = (ServiceGroupContext) getParent();

        ObjectStateUtils.writeObject(out, myParent, "ServiceContext.parent ServiceGroupContext");

    }


    /**
     * Restore the contents of the object that was previously saved.
     * <p/>
     * NOTE: The field data must read back in the same order and type
     * as it was written.  Some data will need to be validated when
     * resurrected.
     *
     * @param in The stream to read the object contents from
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        // set the flag to indicate that the message context is being
        // reconstituted and will need to have certain object references 
        // to be reconciled with the current engine setup
        needsToBeReconciled = true;

        // trace point
        if (LoggingControl.debugLoggingAllowed && log.isTraceEnabled()) {
            log.trace(myClassName + ":readExternal():  BEGIN  bytes available in stream [" +
                in.available() + "]  ");
        }

        //---------------------------------------------------------
        // object level identifiers
        //---------------------------------------------------------

        // serialization version ID
        long suid = in.readLong();

        // revision ID
        int revID = in.readInt();

        // make sure the object data is in a version we can handle
        if (suid != serialVersionUID) {
            throw new ClassNotFoundException(ObjectStateUtils.UNSUPPORTED_SUID);
        }

        // make sure the object data is in a revision level we can handle
        if (revID != REVISION_1) {
            throw new ClassNotFoundException(ObjectStateUtils.UNSUPPORTED_REVID);
        }

        //---------------------------------------------------------
        // various simple fields
        //---------------------------------------------------------

        long time = in.readLong();
        setLastTouchedTime(time);

        cachingOperationContext = in.readBoolean();

        logCorrelationIDString =
                ObjectStateUtils.readString(in, myClassName + ".logCorrelationIDString");

        // trace point
        if (LoggingControl.debugLoggingAllowed && log.isTraceEnabled()) {
            log.trace(myClassName + ":readExternal():  reading input stream for [" +
                logCorrelationIDString + "]  ");
        }

        // EndpointReference targetEPR
        targetEPR = (EndpointReference) ObjectStateUtils.readObject(in, "ServiceContext.targetEPR");

        // EndpointReference myEPR
        myEPR = (EndpointReference) ObjectStateUtils.readObject(in, "ServiceContext.myEPR");

        //---------------------------------------------------------
        // properties
        //---------------------------------------------------------

        HashMap tmpHashMap = ObjectStateUtils.readHashMap(in, "ServiceContext.properties");

        properties = new HashMap();
        if (tmpHashMap != null) {
            setProperties(tmpHashMap);
        }

        //---------------------------------------------------------
        // AxisService
        //---------------------------------------------------------

        // axisService is not usable until the meta data has been reconciled

        ObjectStateUtils.readString(in, "ServiceContext.axisService");

        boolean metaAxisServiceIsActive = in.readBoolean();

        if (metaAxisServiceIsActive == ObjectStateUtils.ACTIVE_OBJECT) {
            metaAxisService = (MetaDataEntry) ObjectStateUtils
                    .readObject(in, "ServiceContext.metaAxisService");
        } else {
            metaAxisService = null;
        }

        //---------------------------------------------------------
        // parent 
        //---------------------------------------------------------

        // ServiceGroupContext is not usable until it has been activated 

        metaParent = (ServiceGroupContext) ObjectStateUtils
                .readObject(in, "ServiceContext.parent ServiceGroupContext");

        //---------------------------------------------------------
        // other
        //---------------------------------------------------------

        // currently not saving this object
        lastOperationContext = null;

        //---------------------------------------------------------
        // done
        //---------------------------------------------------------
    }


    /**
     * This method checks to see if additional work needs to be
     * done in order to complete the object reconstitution.
     * Some parts of the object restored from the readExternal()
     * cannot be completed until we have a configurationContext
     * from the active engine. The configurationContext is used
     * to help this object to plug back into the engine's
     * configuration and deployment objects.
     *
     * @param cc The configuration context object representing the active configuration
     */
    public void activate(ConfigurationContext cc) {
        // see if there's any work to do
        if (!needsToBeReconciled) {
            // return quick
            return;
        }

        // use the supplied configuration context
        configContext = cc;

        // get the axis configuration 
        AxisConfiguration axisConfig = cc.getAxisConfiguration();

        // We previously saved metaAxisService; restore it
        axisService = null;

        if (metaAxisService != null) {
            axisService = ObjectStateUtils.findService(axisConfig, metaAxisService.getClassName(),
                                                       metaAxisService.getQNameAsString());
        }

        // the parent ServiceGroupContext object was saved
        // either use the restored object or sync up with 
        // an existing ServiceGroupContext object
        if (metaParent != null) {
            // find out if a copy of the ServiceGroupContext object exists on this
            // engine where this ServiceContext is being restored/activated
            // if so, use that object instead of the restored object
            // in order to make sure that future changes to group-level 
            // properties are preserved for future operations
            String groupName = metaParent.getId();

            ServiceGroupContext existingSGC = null;

            ServiceGroupContext sgc = cc.getServiceGroupContext(groupName);

            if (existingSGC == null) {
                // could not find an existing ServiceGroupContext
                // use the restored object
                metaParent.activate(cc);

                // set parent 
                this.setParent(metaParent);
            } else {
                // switch over to the existing object
                this.setParent(existingSGC);

                // do a copy of the properties from the restored object
                // to the existing ServiceContext
                // Should the copy be a non-destructive one?  That is,
                // if the key already exists in the properties table of the
                // existing object, should the value be overwritten from the 
                // restored ojbect? For now, the decision is that the state
                // that has been preserved for a saved context object is
                // is important to be restored.
                metaParent.putContextProperties(existingSGC);
            }

        } else {
            // set parent to null
            this.setParent(metaParent);
        }

        // this is another pointer to our parent object
        serviceGroupContext = (ServiceGroupContext) this.getParent();

        if (serviceGroupContext != null) {
            // add the service context to the table
            serviceGroupContext.addServiceContext(this);
        }

        //-------------------------------------------------------
        // done, reset the flag
        //-------------------------------------------------------
        needsToBeReconciled = false;

        // make sure this restored object is in the parent's list
        if (metaParent != null) {
            // make sure this restored object is in the parent's list
            metaParent.addServiceContext(this);
        }

    }

    /**
     * This will do a copy of the properties from this context object
     * to the properties of the specified context object.
     *
     * @param context            The ServiceContext object to hold the merged properties
     * @param doParentProperties Indicates whether to go up the context hierachy
     *                           copy the properties at each level
     */
    public void putContextProperties(ServiceContext context, boolean doParentProperties) {
        if (context != null) {
            // get the current properties on this context object
            Map props = getProperties();

            // copy them to the specified context object
            context.mergeProperties(props);

            if (doParentProperties) {
                ServiceGroupContext mySGC = null;

                if (serviceGroupContext != null) {
                    mySGC = serviceGroupContext;
                } else if (metaParent != null) {
                    mySGC = metaParent;
                }

                if (mySGC != null) {
                    ServiceGroupContext sgc = context.getServiceGroupContext();
                    mySGC.putContextProperties(sgc);
                }
            }
        }
    }


    /**
     * Compares key parts of the state from the current instance of
     * this class with the specified instance to see if they are
     * equivalent.
     * <p/>
     * This differs from the java.lang.Object.equals() method in
     * that the equals() method generally looks at both the
     * object identity (location in memory) and the object state
     * (data).
     * <p/>
     *
     * @param ctx
     * @return TRUE if this object is equivalent with the specified object
     *         that is, key fields match
     *         FALSE, otherwise
     */
    public boolean isEquivalent(ServiceContext ctx) {
        // NOTE: the input object is expected to exist (ie, be non-null)

        if (!this.axisService.equals(ctx.getAxisService())) {
            return false;
        }


        EndpointReference targetEPR2 = ctx.getTargetEPR();

        if ((this.targetEPR != null) && (targetEPR2 != null)) {
            if (!this.targetEPR.isEquivalent(targetEPR2)) {
                return false;
            }
        } else if ((this.targetEPR == null) && (targetEPR2 == null)) {
            // keep going
        } else {
            // one of the objects is null
            return false;
        }

        EndpointReference myEPR2 = ctx.getMyEPR();

        if ((this.myEPR != null) && (myEPR2 != null)) {
            if (!this.myEPR.isEquivalent(myEPR2)) {
                return false;
            }
        } else if ((this.myEPR == null) && (myEPR2 == null)) {
            // keep going
        } else {
            // one of the objects is null
            return false;
        }

        // TODO: consider checking the parent objects for equivalency

        // TODO: consider checking fields from the super class for equivalency

        return true;
    }


    /**
     * Get the ID associated with this object instance.
     *
     * @return A string that can be output to a log file as an identifier
     *         for this object instance.  It is suitable for matching related log
     *         entries.
     */
    public String getLogCorrelationIDString() {
        return logCorrelationIDString;
    }


    /**
     * Trace a warning message, if needed, indicating that this
     * object needs to be activated before accessing certain fields.
     *
     * @param methodname The method where the warning occurs
     */
    private void checkActivateWarning(String methodname) {
        if (needsToBeReconciled) {
            if (LoggingControl.debugLoggingAllowed && log.isDebugEnabled()) {
                log.debug(logCorrelationIDString + ":" + methodname + "(): ****WARNING**** "
                        + myClassName + ".activate(configurationContext) needs to be invoked.");
            }
        }
    }

    public ConfigurationContext getRootContext() {
        return configContext;
    }


}

⌨️ 快捷键说明

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