001: // @@
002: // @@
003: /*
004: * Wi.Ser Framework
005: *
006: * Version: 1.8.1, 20-September-2007
007: * Copyright (C) 2005 Dirk von der Weiden <dvdw@imail.de>
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library located in LGPL.txt in the
021: * license directory; if not, write to the
022: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
023: * Boston, MA 02111-1307, USA.
024: *
025: * If this agreement does not cover your requirements, please contact us
026: * via email to get detailed information about the commercial license
027: * or our service offerings!
028: *
029: */
030: // @@
031: package de.ug2t.kernel;
032:
033: import java.lang.ref.*;
034: import java.util.*;
035:
036: public class KeObjectStorage {
037: private final Map pem_selByName = new HashMap();
038: private final Map pem_selByObj = new HashMap();
039: private final Map pem_remNames = new HashMap();
040: private int pem_autoDelete = KeRegisteredObject.AUTO_DELETE_DISABLED;
041: private boolean pem_register = true;
042: private KeFiFo pem_messages = new KeFiFo();
043: private ArrayList pem_regListeners = null;
044:
045: protected synchronized ArrayList pcmf_getRegListeners() {
046: if (this .pem_regListeners == null)
047: return (null);
048:
049: return (new ArrayList(this .pem_regListeners));
050: }
051:
052: protected synchronized void pdmf_addRegListener(
053: IKeRegistrationListener xListen) {
054: if (this .pem_regListeners == null)
055: this .pem_regListeners = new ArrayList();
056:
057: this .pem_regListeners.add(xListen);
058: }
059:
060: protected synchronized void pdmf_removeRegListener(
061: IKeRegistrationListener xListen) {
062: if (this .pem_regListeners == null)
063: return;
064:
065: this .pem_regListeners.remove(xListen);
066: if (this .pem_regListeners.size() == 0)
067: this .pem_regListeners = null;
068: }
069:
070: protected void pdmf_execRegListeners(Object xObj) {
071: if (this .pem_regListeners == null)
072: return;
073:
074: synchronized (this ) {
075: Iterator l_it = this .pem_regListeners.iterator();
076: while (l_it.hasNext())
077: ((IKeRegistrationListener) l_it.next())
078: .pcmf_registered(xObj);
079: }
080: }
081:
082: protected void pdmf_execUnRegListeners(Object xObj) {
083: if (this .pem_regListeners == null)
084: return;
085:
086: synchronized (this ) {
087: Iterator l_it = this .pem_regListeners.iterator();
088: while (l_it.hasNext())
089: ((IKeRegistrationListener) l_it.next())
090: .pcmf_unregistered(xObj);
091: }
092: }
093:
094: protected void pdmf_execDeletedListeners(Object xObj) {
095: if (this .pem_regListeners == null)
096: return;
097:
098: synchronized (this ) {
099: Iterator l_it = this .pem_regListeners.iterator();
100: while (l_it.hasNext())
101: ((IKeRegistrationListener) l_it.next())
102: .pcmf_deleted(xObj);
103: }
104: }
105:
106: protected void pdmf_pushMessage(KeIPCMessage xMess) {
107: this .pem_messages.pcmf_addObj(xMess);
108: }
109:
110: protected void pdmf_clearMessages() {
111: this .pem_messages.pcmf_clear();
112: }
113:
114: protected KeIPCMessage pdmf_getMessage() {
115: return ((KeIPCMessage) this .pem_messages.pcmf_getObj());
116: }
117:
118: public void pcmf_setRegister(boolean xReg) {
119: this .pem_register = xReg;
120: }
121:
122: public boolean pcmf_getRegister() {
123: return (this .pem_register);
124: }
125:
126: public void pcmf_setAutoDelete(int xAutoDelete) {
127: this .pem_autoDelete = xAutoDelete;
128: };
129:
130: public int pcmf_getAutoDelete() {
131: return (this .pem_autoDelete);
132: };
133:
134: public synchronized String pcmf_getStorageStaticticString() {
135: return ("Storage => fw: " + pem_selByName.size() + ", non fw: "
136: + pem_selByObj.size() + ", remote access: " + pem_remNames
137: .size());
138: };
139:
140: public synchronized int pcmf_getObjectCount() {
141: return (this .pem_selByName.size());
142: };
143:
144: public synchronized String pcmf_getStorageStaticByClassString() {
145: String l_ret = "\n------------------------------------------------------------------\n";
146: l_ret += "objectStorage class statistic start\n";
147: l_ret += "------------------------------------------------------------------\n";
148:
149: TreeMap l_all = new TreeMap();
150: Iterator l_it = this .pem_selByName.values().iterator();
151: Object l_obj = null;
152: Integer l_cnt = null;
153: int l_weak = 0;
154: while (l_it.hasNext()) {
155: l_obj = l_it.next();
156:
157: if (l_obj instanceof Reference
158: && (((Reference) l_obj).get() != null))
159: l_weak++;
160:
161: l_cnt = (Integer) l_all
162: .get((l_obj instanceof Reference ? (((Reference) l_obj)
163: .get() == null ? l_obj.getClass()
164: : ((Reference) l_obj).get().getClass())
165: : l_obj.getClass()).toString());
166: if (l_cnt == null)
167: l_all
168: .put(
169: (l_obj instanceof Reference ? (((Reference) l_obj)
170: .get() == null ? l_obj
171: .getClass()
172: : ((Reference) l_obj).get()
173: .getClass())
174: : l_obj.getClass()).toString(),
175: new Integer(1));
176: else
177: l_all
178: .put(
179: (l_obj instanceof Reference ? (((Reference) l_obj)
180: .get() == null ? l_obj
181: .getClass()
182: : ((Reference) l_obj).get()
183: .getClass())
184: : l_obj.getClass()).toString(),
185: new Integer(l_cnt.intValue() + 1));
186: }
187: l_it = l_all.keySet().iterator();
188: String l_class = null;
189: Integer l_sumcnt = null;
190: while (l_it.hasNext()) {
191: l_class = l_it.next().toString();
192: l_sumcnt = (Integer) l_all.get(l_class);
193: l_ret += l_sumcnt + "\t:" + l_class + "\n";
194: }
195:
196: return (l_ret + "\nWeak/Soft-Refs: " + l_weak + "\n" + "End\n");
197: };
198:
199: public synchronized final void pcmf_putRemName(String xName,
200: Object xObj) {
201: this .pem_remNames.put(xName, xObj);
202: }
203:
204: public synchronized final Object pcmf_getRemName(String xName) {
205: return (this .pem_remNames.get(xName));
206: }
207:
208: public synchronized final Object pcmf_removeRemName(String xName) {
209: return (this .pem_remNames.remove(xName));
210: }
211:
212: public synchronized final ArrayList pcmf_getNameValues() {
213: return (new ArrayList(this .pem_selByName.values()));
214: };
215:
216: public synchronized final boolean pcmf_testObj(Object xObj) {
217: if (this .pem_selByObj.get(xObj) != null)
218: return (true);
219: else
220: return (false);
221: };
222:
223: public synchronized final void pcmf_put(Object xKey, Object xValue) {
224: this .pem_selByName.put(xKey, xValue);
225: this .pem_selByObj.put(xValue, xKey);
226:
227: return;
228: };
229:
230: public synchronized final void pcmf_putToName(Object xKey,
231: Object xValue) {
232: this .pem_selByName.put(xKey, xValue);
233:
234: return;
235: };
236:
237: public synchronized final Object pcmf_removeByName(Object xObj) {
238: Object l_obj = null;
239:
240: l_obj = this .pem_selByName.remove(xObj);
241: if (l_obj != null)
242: this .pem_selByObj.remove(l_obj);
243:
244: return (l_obj);
245: };
246:
247: public synchronized final Object pcmf_removeByObj(Object xObj) {
248: if (xObj instanceof KeRegisteredObject)
249: return (this .pcmf_removeByName(((KeRegisteredObject) xObj)
250: .pcmf_getObjLongName()));
251:
252: Object l_obj = null;
253:
254: l_obj = this .pem_selByObj.remove(xObj);
255: if (l_obj != null)
256: this .pem_selByName.remove(l_obj);
257:
258: return (l_obj);
259: };
260:
261: public synchronized final Object pcmf_getByName(Object xObj) {
262: Object l_obj = null;
263:
264: l_obj = this .pem_selByName.get(xObj);
265: if (l_obj instanceof Reference)
266: l_obj = ((Reference) l_obj).get();
267:
268: return (l_obj);
269: };
270:
271: public synchronized final String pcmf_getName(Object xObj) {
272: if (xObj instanceof KeRegisteredObject)
273: return (((KeRegisteredObject) xObj).pcmf_getObjLongName());
274:
275: String l_obj = null;
276:
277: l_obj = (String) this .pem_selByObj.get(xObj);
278:
279: return (l_obj);
280: };
281:
282: public synchronized ArrayList pcmf_getAllAutoDeletes() {
283: ArrayList l_res = new ArrayList();
284: Iterator l_it = this .pem_selByName.values().iterator();
285: while (l_it.hasNext()) {
286: Object l_obj = l_it.next();
287: if (l_obj instanceof Reference)
288: l_res.add(((Reference) l_obj).get());
289: }
290: return (l_res);
291: }
292: }
|