controlloop m2 sonar fix
[policy/drools-applications.git] / controlloop / m2 / util / src / main / java / org / onap / policy / util / DroolsSessionCommonSerializable.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * util
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.util;
22
23 import java.io.ObjectStreamException;
24 import java.io.Serializable;
25 import java.util.HashMap;
26 import org.onap.policy.drools.core.PolicySession;
27
28 /**
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.
32  */
33 public class DroolsSessionCommonSerializable implements Serializable {
34     private static final long serialVersionUID = 1L;
35
36     // identifies an object within a Drools session
37     private String name;
38
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;
42
43     /**
44      * Constructor - initialize instance, and also store a reference to
45      * the object in a 'PolicySession' adjunct.
46      *
47      * @param name identifies the object within the Drools session
48      * @param object the shared object
49      */
50     public DroolsSessionCommonSerializable(String name, Object object) {
51         this.name = name;
52         this.object = object;
53
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);
59         }
60     }
61
62     /**
63      * Return this object as a String.
64      *
65      * {@inheritDoc}
66      */
67     @Override
68     public String toString() {
69         return "DroolsSessionCommonSerializable[" + name + "]";
70     }
71
72     /**
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).
77      *
78      * @return the local named object (if available), or the deserialized
79      *     object
80      */
81     private Object readResolve() throws ObjectStreamException {
82         Adjunct adjunct = getAdjunct();
83         Object replacementObject;
84
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;
89         }
90
91         // either we couldn't find the adjunct, or couldn't locate the object
92         // within the adjunct
93         return object;
94     }
95
96     /**
97      * This method will:
98      * 1) Locate the 'PolicySession' (only works from within the Drools thread),
99      * 2) Find or create the adjunct.
100      *
101      * @return the adjunct, or 'null' if we aren't running within a
102      *     Drools session
103      */
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);
115             } else {
116                 // found the adjunct -- return it
117                 adjunct = (Adjunct) adj;
118             }
119         }
120         return adjunct;
121     }
122
123     /* ============================================================ */
124
125     /**
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
129      * warnings.
130      */
131     private static class Adjunct extends HashMap<String, Object> {
132         private static final long serialVersionUID = 1L;
133     }
134 }