diff options
Diffstat (limited to 'framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util')
19 files changed, 342 insertions, 223 deletions
diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/AdapterContainerFilter.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/AdapterContainerFilter.java index 2c8b89892..843b32b6b 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/AdapterContainerFilter.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/AdapterContainerFilter.java @@ -1,12 +1,14 @@ /**************************************************************************** * Copyright (c) 2008 Composent, Inc. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ * * Contributors: * Composent, Inc. - initial API and implementation + * + * SPDX-License-Identifier: EPL-2.0 *****************************************************************************/ package org.eclipse.ecf.core.util; diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/BundleClassResolver.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/BundleClassResolver.java index 017e42a4e..7f0467bb8 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/BundleClassResolver.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/BundleClassResolver.java @@ -1,11 +1,14 @@ -/******************************************************************************* - * Copyright (c) 2015 Composent, Inc. and others. All rights reserved. This - * program and the accompanying materials are made available under the terms of - * the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * +/**************************************************************************** + * Copyright (c) 2015 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * * Contributors: Composent, Inc. - initial API and implementation - ******************************************************************************/ + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; import java.io.ObjectStreamClass; diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ClassResolverObjectInputStream.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ClassResolverObjectInputStream.java index 00d35d6be..553d7b6d7 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ClassResolverObjectInputStream.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ClassResolverObjectInputStream.java @@ -1,11 +1,14 @@ -/******************************************************************************* - * Copyright (c) 2015 Composent, Inc. and others. All rights reserved. This - * program and the accompanying materials are made available under the terms of - * the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * +/**************************************************************************** + * Copyright (c) 2015 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * * Contributors: Composent, Inc. - initial API and implementation - ******************************************************************************/ + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; import java.io.*; @@ -80,16 +83,6 @@ public class ClassResolverObjectInputStream extends ObjectInputStream { return this.bundleContext; } - private IClassResolver getClassResolver() { - IClassResolver result = null; - if (this.classResolverST != null) { - this.classResolverST.open(); - result = this.classResolverST.getService(); - this.classResolverST.close(); - } - return result; - } - @SuppressWarnings("unused") @Override protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ConnectedContainerFilter.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ConnectedContainerFilter.java index 1f4e97891..ef28b50c0 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ConnectedContainerFilter.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ConnectedContainerFilter.java @@ -1,12 +1,14 @@ /**************************************************************************** * Copyright (c) 2008 Composent, Inc. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ * * Contributors: * Composent, Inc. - initial API and implementation + * + * SPDX-License-Identifier: EPL-2.0 *****************************************************************************/ package org.eclipse.ecf.core.util; diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ContainerFactoryTracker.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ContainerFactoryTracker.java index 872b875ca..875198ac2 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ContainerFactoryTracker.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ContainerFactoryTracker.java @@ -1,12 +1,15 @@ -/******************************************************************************* -* Copyright (c) 2009 EclipseSource and others. All rights reserved. This -* program and the accompanying materials are made available under the terms of -* the Eclipse Public License v1.0 which accompanies this distribution, and is -* available at http://www.eclipse.org/legal/epl-v10.html -* -* Contributors: -* EclipseSource - initial API and implementation -******************************************************************************/ +/**************************************************************************** + * Copyright (c) 2009 EclipseSource and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * Contributors: + * EclipseSource - initial API and implementation + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; import org.eclipse.ecf.core.IContainerFactory; diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ContainerManagerTracker.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ContainerManagerTracker.java index 5ffb3b081..e9b51d246 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ContainerManagerTracker.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ContainerManagerTracker.java @@ -1,12 +1,15 @@ -/******************************************************************************* -* Copyright (c) 2009 EclipseSource and others. All rights reserved. This -* program and the accompanying materials are made available under the terms of -* the Eclipse Public License v1.0 which accompanies this distribution, and is -* available at http://www.eclipse.org/legal/epl-v10.html -* -* Contributors: -* EclipseSource - initial API and implementation -******************************************************************************/ +/**************************************************************************** + * Copyright (c) 2009 EclipseSource and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * Contributors: + * EclipseSource - initial API and implementation + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; import org.eclipse.ecf.core.IContainerManager; diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/Event.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/Event.java index d1143dbca..d56a2c2a8 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/Event.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/Event.java @@ -1,11 +1,14 @@ -/******************************************************************************* - * Copyright (c) 2004 Composent, Inc. and others. All rights reserved. This - * program and the accompanying materials are made available under the terms of - * the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * +/**************************************************************************** + * Copyright (c) 2004 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * * Contributors: Composent, Inc. - initial API and implementation - ******************************************************************************/ + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; /** diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IClassResolver.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IClassResolver.java index 88972cf9f..a74fab53c 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IClassResolver.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IClassResolver.java @@ -1,11 +1,14 @@ -/******************************************************************************* - * Copyright (c) 2015 Composent, Inc. and others. All rights reserved. This - * program and the accompanying materials are made available under the terms of - * the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * +/**************************************************************************** + * Copyright (c) 2015 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * * Contributors: Composent, Inc. - initial API and implementation - ******************************************************************************/ + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; import java.io.ObjectStreamClass; diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IContainerFilter.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IContainerFilter.java index 10b8bddca..0d566db8c 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IContainerFilter.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IContainerFilter.java @@ -1,12 +1,14 @@ /**************************************************************************** * Copyright (c) 2008 Composent, Inc. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ * * Contributors: * Composent, Inc. - initial API and implementation + * + * SPDX-License-Identifier: EPL-2.0 *****************************************************************************/ package org.eclipse.ecf.core.util; diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IEventProcessor.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IEventProcessor.java index b3d70a402..fd8ba2f0d 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IEventProcessor.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IEventProcessor.java @@ -1,11 +1,14 @@ -/******************************************************************************* - * Copyright (c) 2004 Composent, Inc. and others. All rights reserved. This - * program and the accompanying materials are made available under the terms of - * the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * +/**************************************************************************** + * Copyright (c) 2004 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * * Contributors: Composent, Inc. - initial API and implementation - ******************************************************************************/ + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; /** diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IExceptionHandler.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IExceptionHandler.java index 5801397bc..6aa6ce102 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IExceptionHandler.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/IExceptionHandler.java @@ -1,11 +1,14 @@ -/******************************************************************************* - * Copyright (c) 2004 Composent, Inc. and others. All rights reserved. This - * program and the accompanying materials are made available under the terms of - * the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * +/**************************************************************************** + * Copyright (c) 2004 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * * Contributors: Composent, Inc. - initial API and implementation - ******************************************************************************/ + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; import org.eclipse.core.runtime.IStatus; diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectInputStream.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectInputStream.java index 1d0c70293..c2057b6de 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectInputStream.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectInputStream.java @@ -1,11 +1,14 @@ -/******************************************************************************* - * Copyright (c) 2018 Composent, Inc. and others. All rights reserved. This - * program and the accompanying materials are made available under the terms of - * the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * +/**************************************************************************** + * Copyright (c) 2018, 2020 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * * Contributors: Composent, Inc. - initial API and implementation - ******************************************************************************/ + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; import java.io.*; @@ -20,14 +23,54 @@ import org.osgi.service.log.LogService; */ public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObjectStreamConstants { - protected final ObjectInputStream in; + protected ObjectInputStream in; protected final Bundle b; protected LogService logger; + /** + * @since 3.10 + */ + ClassLoader classLoader; + + class ReplaceableObjectInputStream extends ObjectInputStream { + public ReplaceableObjectInputStream(InputStream ins) throws IOException { + super(ins); + enableResolveObject(true); + } + + @Override + protected Object resolveObject(Object obj) throws IOException { + if (obj instanceof SerVersion) { + return ((SerVersion) obj).toVersion(); + } + if (obj instanceof SerDTO) { + SerDTO serDTO = (SerDTO) obj; + String className = serDTO.getClassname(); + Class<?> clazz = null; + try { + clazz = loadClass(className); + } catch (Exception e) { + throw new IOException("Could not load class for instance of SerDTO with className=" + className); //$NON-NLS-1$ + } + return serDTO.readObject(clazz); + } + return super.resolveObject(obj); + } + + @Override + protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { + try { + return loadClass(desc.getName()); + } catch (Exception e) { + return super.resolveClass(desc); + } + } + } + public OSGIObjectInputStream(Bundle b, InputStream in, LogService logger) throws IOException { super(); this.b = b; - this.in = new ObjectInputStream(in); + this.in = new ReplaceableObjectInputStream(in); this.logger = logger; } @@ -35,6 +78,13 @@ public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObje this(b, in, null); } + /** + * @since 3.10 + */ + public void setClassLoader(ClassLoader cl) { + this.classLoader = cl; + } + public void setLogService(LogService log) { this.logger = log; } @@ -48,7 +98,15 @@ public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObje } protected Class loadClass(String classname) throws ClassNotFoundException { + ClassLoader cl = this.classLoader; Bundle bundle = b; + if (cl != null) { + try { + return Class.forName(classname, false, cl); + } catch (Exception e) { + // Try bundle loading + } + } return (bundle == null) ? Class.forName(classname) : bundle.loadClass(classname); } @@ -77,17 +135,13 @@ public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObje final byte type = in.readByte(); switch (type) { case C_NULL : // null - trace("null"); //$NON-NLS-1$ return null; case C_SER : // Serializable - trace("readSerializedObject"); //$NON-NLS-1$ return readSerializedObject(); case C_VER : // Version - trace("readVersion"); //$NON-NLS-1$ return Version.parseVersion(in.readUTF()); case C_ARRAY : // Object[] // read array length - trace("readArray"); //$NON-NLS-1$ int ol = in.readInt(); // read component type and create array for that component type Class<?> clazz = getClassForType(in.readUTF()); @@ -95,11 +149,7 @@ public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObje for (int i = 0; i < ol; i++) Array.set(oresult, i, readObjectOverride()); return oresult; - case C_DTO : // DTO - trace("readDTO"); //$NON-NLS-1$ - return readDTO(); case C_DICT : // Dictionary - trace("readDictionary"); //$NON-NLS-1$ Class<?> dictClazz = loadClass(in.readUTF()); Dictionary dict = null; Constructor cons; @@ -120,7 +170,6 @@ public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObje case C_MAP : // Map // read map length int ms = in.readInt(); - trace("readMap=" + ms); //$NON-NLS-1$ Map mr = new HashMap(); for (int i = 0; i < ms; i++) { Object key = readObjectOverride(); @@ -130,28 +179,24 @@ public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObje return mr; case C_LIST : // List int lsize = in.readInt(); - trace("readList=" + lsize); //$NON-NLS-1$ List l = new ArrayList(lsize); for (int i = 0; i < lsize; i++) l.add(readObjectOverride()); return l; case C_SET : // Set int ssize = in.readInt(); - trace("readSet=" + ssize); //$NON-NLS-1$ Set s = new HashSet(ssize); for (int i = 0; i < ssize; i++) s.add(readObjectOverride()); return s; case C_COLL : // Collection int csize = in.readInt(); - trace("readCol=" + csize); //$NON-NLS-1$ Collection c = new ArrayList(csize); for (int i = 0; i < csize; i++) c.add(readObjectOverride()); return c; case C_ITER : // Iterable int isize = in.readInt(); - trace("readIter=" + isize); //$NON-NLS-1$ List itr = new ArrayList(isize); for (int i = 0; i < isize; i++) itr.add(readObjectOverride()); @@ -159,42 +204,32 @@ public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObje case C_EXTER : // Externalizable return in.readObject(); case C_STRING : // String - trace("readString"); //$NON-NLS-1$ return in.readUTF(); case C_LONG : case C_OLONG : - trace("readLong"); //$NON-NLS-1$ return in.readLong(); case C_INT : case C_OINT : - trace("readInt"); //$NON-NLS-1$ return in.readInt(); case C_SHORT : case C_OSHORT : - trace("readShort"); //$NON-NLS-1$ return in.readShort(); case C_BOOL : case C_OBOOL : - trace("readBool"); //$NON-NLS-1$ return in.readBoolean(); case C_BYTE : case C_OBYTE : - trace("readByte"); //$NON-NLS-1$ return in.readByte(); case C_CHAR : case C_OCHAR : - trace("readChar"); //$NON-NLS-1$ return in.readChar(); case C_DOUBLE : case C_ODOUBLE : - trace("readDouble"); //$NON-NLS-1$ return in.readDouble(); case C_FLOAT : case C_OFLOAT : - trace("readFloat"); //$NON-NLS-1$ return in.readFloat(); case C_ENUM : - trace("readEnum"); //$NON-NLS-1$ return Enum.valueOf(loadClass(in.readUTF()), in.readUTF()); case C_OBJECT : return readNonSerializedObject(); @@ -203,25 +238,6 @@ public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObje } } - private Object readDTO() throws IOException, ClassNotFoundException { - Class<?> clazz = loadClass(in.readUTF()); - Object result = null; - try { - result = clazz.newInstance(); - for (Field f : clazz.getFields()) { - final int mod = f.getModifiers(); - // If it's static or transient then ignore - if (Modifier.isStatic(mod) || Modifier.isTransient(mod)) - continue; - // Else read and set value of field - f.set(result, readObjectOverride()); - } - } catch (Exception e) { - throw new IOException("Cannot deserialize DTO because of exception: " + e.getMessage()); //$NON-NLS-1$ - } - return result; - } - protected Object readExternalizable() throws ClassNotFoundException, IOException { return in.readObject(); } @@ -254,7 +270,7 @@ public class OSGIObjectInputStream extends ObjectInputStream implements OSGIObje protected Object createInstance(Class<?> clazz) throws IOException { try { - return clazz.newInstance(); + return clazz.getDeclaredConstructor().newInstance(); } catch (Exception e) { throw new IOException("Could create new instance of class=" + clazz.getName() + ". Class must have public no-arg constructor"); //$NON-NLS-1$ //$NON-NLS-2$ } diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectOutputStream.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectOutputStream.java index 25324866a..c6ab29cd7 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectOutputStream.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectOutputStream.java @@ -1,17 +1,19 @@ -/******************************************************************************* - * Copyright (c) 2018 Composent, Inc. and others. All rights reserved. This - * program and the accompanying materials are made available under the terms of - * the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * +/**************************************************************************** + * Copyright (c) 2018 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * * Contributors: Composent, Inc. - initial API and implementation - ******************************************************************************/ + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; import java.io.*; import java.lang.reflect.*; import java.util.*; -import org.osgi.dto.DTO; import org.osgi.framework.Version; import org.osgi.service.log.LogService; @@ -24,9 +26,27 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb protected LogService logger; protected boolean allowNonSerializable = false; + class ReplaceableObjectOutputStream extends ObjectOutputStream { + + public ReplaceableObjectOutputStream(OutputStream out) throws IOException { + super(out); + enableReplaceObject(true); + } + + @Override + protected Object replaceObject(Object obj) throws IOException { + if (obj instanceof Version) { + return new SerVersion(((Version) obj)); + } else if (!(obj instanceof Serializable)) { + return new SerDTO(obj); + } + return super.replaceObject(obj); + } + } + public OSGIObjectOutputStream(OutputStream out, boolean allowNonSerializable, LogService log) throws IOException { super(); - this.out = new ObjectOutputStream(out); + this.out = new ReplaceableObjectOutputStream(out); this.allowNonSerializable = allowNonSerializable; this.logger = log; } @@ -48,7 +68,6 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb } protected void writeExternalizable(Externalizable obj, Class<?> clazz) throws IOException { - trace("writeExternalizable " + clazz.getName()); //$NON-NLS-1$ out.writeObject(obj); } @@ -99,8 +118,6 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb } protected void writeNonSerializable(Object obj, Class<?> clazz) throws IOException { - // lookup object stream class - trace("writeNonSerializable " + clazz.getName()); //$NON-NLS-1$ // write class name out.writeObject(clazz.getName()); writeFields(obj, clazz); @@ -118,7 +135,6 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb } Class<?> clazz = obj.getClass(); if (clazz.isArray()) { - trace("writing array"); //$NON-NLS-1$ out.writeByte(C_ARRAY); int len = Array.getLength(obj); // write length @@ -130,88 +146,70 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb writeObjectOverride(Array.get(obj, i)); return; } else if (obj instanceof Long) { - trace("writing Long"); //$NON-NLS-1$ if (clazz.isPrimitive()) { - trace("writing long"); //$NON-NLS-1$ out.writeByte(C_LONG); } else { - trace("writing Long"); //$NON-NLS-1$ out.writeByte(C_OLONG); } out.writeLong((Long) obj); return; } else if (obj instanceof Integer) { if (clazz.isPrimitive()) { - trace("writing int"); //$NON-NLS-1$ out.writeByte(C_INT); } else { - trace("writing Integer"); //$NON-NLS-1$ out.writeByte(C_OINT); } out.writeInt((Integer) obj); return; } else if (obj instanceof Short) { if (clazz.isPrimitive()) { - trace("writing short"); //$NON-NLS-1$ out.writeByte(C_SHORT); } else { - trace("writing Short"); //$NON-NLS-1$ out.writeByte(C_OSHORT); } out.writeShort((Short) obj); return; } else if (obj instanceof Boolean) { if (clazz.isPrimitive()) { - trace("writing bool"); //$NON-NLS-1$ out.writeByte(C_BOOL); } else { - trace("writing Boolean"); //$NON-NLS-1$ out.writeByte(C_OBOOL); } out.writeBoolean((Boolean) obj); return; } else if (obj instanceof Byte) { if (clazz.isPrimitive()) { - trace("writing byte"); //$NON-NLS-1$ out.writeByte(C_BYTE); } else { - trace("writing Byte"); //$NON-NLS-1$ out.writeByte(C_OBYTE); } out.writeByte((Byte) obj); return; } else if (obj instanceof Character) { if (clazz.isPrimitive()) { - trace("writing char"); //$NON-NLS-1$ out.writeByte(C_CHAR); } else { - trace("writing Character"); //$NON-NLS-1$ out.writeByte(C_OCHAR); } out.writeChar((Character) obj); return; } else if (obj instanceof Float) { if (clazz.isPrimitive()) { - trace("writing float"); //$NON-NLS-1$ out.writeByte(C_FLOAT); } else { - trace("writing Float"); //$NON-NLS-1$ out.writeByte(C_OFLOAT); } out.writeFloat((Float) obj); return; } else if (obj instanceof Double) { if (clazz.isPrimitive()) { - trace("writing double"); //$NON-NLS-1$ out.writeByte(C_DOUBLE); } else { - trace("writing Double"); //$NON-NLS-1$ out.writeByte(C_ODOUBLE); } out.writeDouble((Double) obj); return; } else if (obj instanceof String) { - trace("writing String"); //$NON-NLS-1$ out.writeByte(C_STRING); out.writeUTF((String) obj); return; @@ -222,7 +220,6 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb Dictionary dict = (Dictionary) obj; // write size int ds = dict.size(); - trace("writing Dictionary=" + ds); //$NON-NLS-1$ out.writeInt(ds); // for each element in Map for (Enumeration e = dict.keys(); e.hasMoreElements();) { @@ -236,7 +233,6 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb Map map = (Map) obj; // write size int size = map.size(); - trace("writing Map=" + size); //$NON-NLS-1$ out.writeInt(size); // for each element in Map for (Object key : map.keySet()) { @@ -251,7 +247,6 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb List list = (List) obj; // write size int size = list.size(); - trace("writing List=" + size); //$NON-NLS-1$ out.writeInt(size); // write each element for (Object item : list) @@ -262,7 +257,6 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb Set set = (Set) obj; // write size int size = set.size(); - trace("writing Set=" + size); //$NON-NLS-1$ out.writeInt(size); // then elements for (Object item : set) @@ -273,7 +267,6 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb Collection col = (Collection) obj; // write size int size = col.size(); - trace("writing col=" + size); //$NON-NLS-1$ out.writeInt(size); // then elements for (Object item : col) @@ -288,7 +281,6 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb Object v : itr) size++; // write size - trace("writing Iterable=" + size); //$NON-NLS-1$ out.writeInt(size); // write elements for (Object item : itr) @@ -308,38 +300,10 @@ public class OSGIObjectOutputStream extends ObjectOutputStream implements OSGIOb writeSerializable(obj, clazz); return; } else if (obj instanceof Version) { - trace("writing Version"); //$NON-NLS-1$ - out.writeByte(C_VER); - out.writeUTF(((Version) obj).toString()); - return; - } else if (obj instanceof DTO) { - out.writeByte(C_DTO); - writeDTO(obj, clazz); - return; + writeObjectOverride(new SerVersion((Version) obj)); } else { - if (allowNonSerializable) { - out.writeByte(C_OBJECT); - writeNonSerializable(obj, clazz); - return; - } - throw new NotSerializableException("Cannot serialize instance of class=" + clazz.getName()); //$NON-NLS-1$ - } - } - - private void writeDTO(Object obj, Class<?> clazz) throws IOException { - trace("writing DTO"); //$NON-NLS-1$ - // Write out class name - out.writeUTF(clazz.getName()); - for (Field f : clazz.getFields()) { - final int mod = f.getModifiers(); - if (Modifier.isStatic(mod) || Modifier.isTransient(mod)) - continue; - try { - writeObjectOverride(f.get(obj)); - } catch (Exception e) { - // Should not happen for DTO - throw new NotSerializableException(clazz.getName()); - } + writeObjectOverride(new SerDTO(obj)); + return; } } diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectStreamConstants.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectStreamConstants.java index b1cfaaa77..7163854ba 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectStreamConstants.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/OSGIObjectStreamConstants.java @@ -1,11 +1,14 @@ -/******************************************************************************* - * Copyright (c) 2018 Composent, Inc. and others. All rights reserved. This - * program and the accompanying materials are made available under the terms of - * the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html - * +/**************************************************************************** + * Copyright (c) 2018 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * * Contributors: Composent, Inc. - initial API and implementation - ******************************************************************************/ + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util; /** diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/Proxy.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/Proxy.java index 786549ac4..8b99e8520 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/Proxy.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/Proxy.java @@ -1,12 +1,14 @@ /**************************************************************************** * Copyright (c) 2004 Composent, Inc. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ * * Contributors: * Composent, Inc. - initial API and implementation + * + * SPDX-License-Identifier: EPL-2.0 *****************************************************************************/ package org.eclipse.ecf.core.util; diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ProxyAddress.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ProxyAddress.java index 152b9d02e..d393ac181 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ProxyAddress.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/ProxyAddress.java @@ -1,12 +1,14 @@ /**************************************************************************** * Copyright (c) 2004 Composent, Inc. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ * * Contributors: * Composent, Inc. - initial API and implementation + * + * SPDX-License-Identifier: EPL-2.0 *****************************************************************************/ package org.eclipse.ecf.core.util; diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/SerDTO.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/SerDTO.java new file mode 100644 index 000000000..876888455 --- /dev/null +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/SerDTO.java @@ -0,0 +1,79 @@ +/**************************************************************************** + * Copyright (c) 2021 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * Contributors: + * Composent, Inc. - initial API and implementation + * + *****************************************************************************/ +package org.eclipse.ecf.core.util; + +import java.io.IOException; +import java.io.Serializable; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.HashMap; +import java.util.Map; + +/** + * @since 3.10 + */ +public class SerDTO implements Serializable { + + private static final long serialVersionUID = -1739849704193630352L; + private String className; + private Map<String, Object> fields; + + public SerDTO(Object obj) { + Class<?> clazz = obj.getClass(); + this.className = clazz.getName(); + this.fields = new HashMap<String, Object>(); + for (Field f : clazz.getFields()) { + final int mod = f.getModifiers(); + if (Modifier.isStatic(mod) || Modifier.isTransient(mod)) + continue; + Object value = null; + try { + value = f.get(obj); + } catch (Exception e) { + // + } + if (value != null) { + fields.put(f.getName(), value); + } + } + } + + public String getClassname() { + return this.className; + } + + public Map<String, Object> getFields() { + return fields; + } + + public Object readObject(Class<?> clazz) throws IOException { + Object result = null; + try { + result = clazz.getConstructor(new Class[] {}).newInstance(); + for (Field f : clazz.getFields()) { + Object v = fields.get(f.getName()); + if (v != null) { + f.setAccessible(true); + try { + f.set(result, v); + } catch (Exception e) { + // ignore + } + } + } + } catch (Throwable e) { + throw new IOException("Unexpected exception reading DTO in SerDTO.readObject: " + e.getLocalizedMessage()); //$NON-NLS-1$ + } + return result; + + } +} diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/SerVersion.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/SerVersion.java new file mode 100644 index 000000000..dd20c732c --- /dev/null +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/SerVersion.java @@ -0,0 +1,31 @@ +/**************************************************************************** + * Copyright (c) 2021 Composent, Inc. and others. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * Contributors: + * Composent, Inc. - initial API and implementation + * + *****************************************************************************/ +package org.eclipse.ecf.core.util; + +import java.io.Serializable; +import org.osgi.framework.Version; + +/** + * @since 3.10 + */ +public final class SerVersion implements Serializable { + private static final long serialVersionUID = 4785952431053485808L; + private String versionStr; + + public SerVersion(Version v) { + this.versionStr = v.toString(); + } + + public Version toVersion() { + return Version.parseVersion(this.versionStr); + } +}
\ No newline at end of file diff --git a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/reflection/ClassUtil.java b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/reflection/ClassUtil.java index 128ade165..92456b1a8 100644 --- a/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/reflection/ClassUtil.java +++ b/framework/bundles/org.eclipse.ecf/src/org/eclipse/ecf/core/util/reflection/ClassUtil.java @@ -1,13 +1,15 @@ -/******************************************************************************* +/**************************************************************************** * Copyright (c) 2010 Markus Alexander Kuppe. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ * * Contributors: * Markus Alexander Kuppe (ecf-dev_eclipse.org <at> lemmster <dot> de) - initial API and implementation - ******************************************************************************/ + * + * SPDX-License-Identifier: EPL-2.0 + *****************************************************************************/ package org.eclipse.ecf.core.util.reflection; import java.lang.reflect.Method; |