2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.util;
23 import java.io.ObjectStreamException;
24 import java.io.Serializable;
25 import java.util.HashMap;
26 import org.onap.policy.drools.core.PolicySession;
29 * This class provides a way to serialize/deserialize objects by locating
30 * an already existing object on the remote end, and using that instead. It
31 * is useful for objects that may be shared by multiple transactions.
33 public class DroolsSessionCommonSerializable implements Serializable {
34 private static final long serialVersionUID = 1L;
36 // identifies an object within a Drools session
39 // the object is serialized, but is only used if the corresponding object
40 // on the remote end can't be located for some reason
41 private Object object;
44 * Constructor - initialize instance, and also store a reference to
45 * the object in a 'PolicySession' adjunct.
47 * @param name identifies the object within the Drools session
48 * @param object the shared object
50 public DroolsSessionCommonSerializable(String name, Object object) {
54 // store a reference to the object within the adjunct
55 // (only works if we can locate the Drools session)
56 Adjunct adjunct = getAdjunct();
57 if (adjunct != null) {
58 adjunct.put(name, object);
63 * Return this object as a String.
68 public String toString() {
69 return "DroolsSessionCommonSerializable[" + name + "]";
73 * This method runs as part of deserialization. If we are able to locate
74 * the 'PolicySession' and adjunct, and fetch the replacement object from
75 * the adjunct, that is used. Otherwise, the deserialized object is used
76 * (which is likely a duplicate).
78 * @return the local named object (if available), or the deserialized
81 private Object readResolve() throws ObjectStreamException {
82 Adjunct adjunct = getAdjunct();
83 Object replacementObject;
85 if (adjunct != null && (replacementObject = adjunct.get(name)) != null) {
86 // we found the adjunct, as well as the replacement object -- use
87 // the replacement object
88 return replacementObject;
91 // either we couldn't find the adjunct, or couldn't locate the object
98 * 1) Locate the 'PolicySession' (only works from within the Drools thread),
99 * 2) Find or create the adjunct.
101 * @return the adjunct, or 'null' if we aren't running within a
104 private Adjunct getAdjunct() {
105 // PolicySession - this only works from within the Drools thread
106 PolicySession session = PolicySession.getCurrentSession();
107 Adjunct adjunct = null;
108 if (session != null) {
109 // we found the 'PolicySession' -- now, look for the adjunct
110 Object adj = session.getAdjunct(Adjunct.class);
111 if (!(adj instanceof Adjunct)) {
112 // adjunct does not exist, or has the wrong type -- create it
113 adjunct = new Adjunct();
114 session.setAdjunct(Adjunct.class, adjunct);
116 // found the adjunct -- return it
117 adjunct = (Adjunct) adj;
123 /* ============================================================ */
126 * While 'HashMap<String, Object>' could be used directly instead of defining
127 * a subclass, you can't do run-time type checking of a parameterized type.
128 * As a result, the 'getAdjunct' method (above) would get compile-time
131 private static class Adjunct extends HashMap<String, Object> {
132 private static final long serialVersionUID = 1L;