felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1796661 - in /felix/trunk/osgi-r7/configurator/src: main/java/org/apache/felix/configurator/impl/ main/java/org/apache/felix/configurator/impl/conversion/ main/java/org/apache/felix/configurator/impl/json/ main/java/org/apache/felix/config...
Date Mon, 29 May 2017 16:17:03 GMT
Author: cziegeler
Date: Mon May 29 16:17:02 2017
New Revision: 1796661

URL: http://svn.apache.org/viewvc?rev=1796661&view=rev
Log:
Refactor for better reusability

Added:
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/BinUtil.java
  (with props)
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/TypeConverter.java
      - copied, changed from r1796660, felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/TypeConverter.java
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ReflectionUtil.java
  (with props)
    felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/conversion/
    felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/conversion/TypeConverterTest.java
      - copied, changed from r1796660, felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/TypeConverterTest.java
Removed:
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/TypeConverter.java
    felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/TypeConverterTest.java
Modified:
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Util.java
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/AbstractState.java
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/Config.java
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ConfigList.java
    felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/State.java
    felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/json/JSONUtilTest.java

Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java?rev=1796661&r1=1796660&r2=1796661&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Configurator.java
Mon May 29 16:17:02 2017
@@ -32,6 +32,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
 
+import org.apache.felix.configurator.impl.conversion.BinUtil;
 import org.apache.felix.configurator.impl.json.JSONUtil;
 import org.apache.felix.configurator.impl.logger.SystemLogger;
 import org.apache.felix.configurator.impl.model.BundleState;
@@ -85,7 +86,14 @@ public class Configurator {
         this.bundleContext = bc;
         this.configAdminReferences = configAdminReferences;
         this.activeEnvironments = Util.getActiveEnvironments(bc);
-        this.state = State.createOrReadState(bundleContext);
+        State s = null;
+        try {
+            s = State.createOrReadState(bundleContext);
+        } catch ( final ClassNotFoundException | IOException e ) {
+            SystemLogger.error("Unable to read persisted state from " + State.FILE_NAME,
e);
+            s = new State();
+        }
+        this.state = s;
         this.state.changeEnvironments(this.activeEnvironments);
         this.tracker = new org.osgi.util.tracker.BundleTracker<>(this.bundleContext,
                 Bundle.ACTIVE|Bundle.STARTING|Bundle.STOPPING|Bundle.RESOLVED|Bundle.INSTALLED,
@@ -187,26 +195,26 @@ public class Configurator {
         if ( dirPath != null ) {
             final File dir = new File(dirPath);
             if ( dir.exists() && dir.isDirectory() ) {
-                Util.binDirectory = dir;
+                BinUtil.binDirectory = dir;
             } else if ( dir.exists() ) {
                 SystemLogger.error("Directory property is pointing at a file not a dir: "
+ dirPath + ". Using default path.");
             } else {
                 try {
                     if ( dir.mkdirs() ) {
-                        Util.binDirectory = dir;
+                        BinUtil.binDirectory = dir;
                     }
                 } catch ( final SecurityException se ) {
                     // ignore
                 }
-                if ( Util.binDirectory == null ) {
+                if ( BinUtil.binDirectory == null ) {
                     SystemLogger.error("Unable to create a directory at: " + dirPath + ".
Using default path.");
                 }
             }
         }
-        if ( Util.binDirectory == null ) {
-            Util.binDirectory = this.bundleContext.getDataFile("binaries" + File.separatorChar
+ ".check");
-            Util.binDirectory = Util.binDirectory.getParentFile();
-            Util.binDirectory.mkdirs();
+        if ( BinUtil.binDirectory == null ) {
+            BinUtil.binDirectory = this.bundleContext.getDataFile("binaries" + File.separatorChar
+ ".check");
+            BinUtil.binDirectory = BinUtil.binDirectory.getParentFile();
+            BinUtil.binDirectory.mkdirs();
         }
 
         // before we start the tracker we process all available bundles and initial configuration
@@ -228,10 +236,12 @@ public class Configurator {
                     } catch (final MalformedURLException e) {
                     }
                     if ( url != null ) {
-                        final String contents = Util.getResource(urlString, url);
-                        if ( contents != null ) {
+                        try {
+                            final String contents = JSONUtil.getResource(urlString, url);
                             files.put(urlString, contents);
                             hashes.add(Util.getSHA256(contents.trim()));
+                        } catch ( final IOException ioe ) {
+                            SystemLogger.error("Unable to read " + urlString, ioe);
                         }
                     }
                 }
@@ -354,9 +364,14 @@ public class Configurator {
 
                 if ( configList.hasChanges() ) {
                     process(configList);
-                    State.writeState(this.bundleContext, state);
+                    try {
+                        State.writeState(this.bundleContext, state);
+                    } catch ( final IOException ioe) {
+                        SystemLogger.error("Unable to persist state to " + State.FILE_NAME,
ioe);
+                    }
                 }
             }
+
         } finally {
             if ( coordination != null ) {
                 CoordinatorUtil.endCoordination(coordination);

Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Util.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Util.java?rev=1796661&r1=1796660&r2=1796661&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Util.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Util.java
Mon May 29 16:17:02 2017
@@ -18,24 +18,13 @@
  */
 package org.apache.felix.configurator.impl;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
-import java.lang.reflect.Field;
-import java.net.URL;
-import java.net.URLConnection;
-import java.net.URLEncoder;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
-import java.util.UUID;
 
 import org.apache.felix.configurator.impl.logger.SystemLogger;
 import org.osgi.framework.Bundle;
@@ -54,8 +43,6 @@ public class Util {
 
     private static final String DEFAULT_PATH = "OSGI-INF/configurator";
 
-    public static volatile File binDirectory;
-
     /**
      * Check if the bundle contains configurations for the configurator
      * @param bundle The bundle
@@ -148,28 +135,7 @@ public class Util {
         return isValid;
     }
 
-    /**
-     * Set a (final) field during deserialization.
-     */
-    public static void setField(final Object obj, final String name, final Object value)
-    throws IOException {
-        Class<?> clazz = obj.getClass();
-        while ( clazz != null ) {
-            try {
-                final Field field = clazz.getDeclaredField(name);
-                field.setAccessible(true);
-                field.set(obj, value);
-                return;
-            } catch (final NoSuchFieldException e) {
-                clazz = clazz.getSuperclass();
-            } catch (final SecurityException | IllegalArgumentException  | IllegalAccessException
e) {
-                throw (IOException)new IOException().initCause(e);
-            }
-        }
-        throw new IOException("Field " + name + " not found in class " + obj.getClass());
-    }
-
-    public static String getSHA256(final String value) {
+   public static String getSHA256(final String value) {
         try {
             StringBuilder builder = new StringBuilder();
             MessageDigest md = MessageDigest.getInstance("SHA-256");
@@ -181,71 +147,4 @@ public class Util {
             throw new RuntimeException(e);
         }
     }
-
-    /**
-     * Read the contents of a resource, encoded as UTF-8
-     * @param name The resource name
-     * @param url The resource URL
-     * @return The contents or {@code null}
-     */
-    public static String getResource(final String name, final URL url) {
-        URLConnection connection = null;
-        try {
-            connection = url.openConnection();
-
-            try(final BufferedReader in = new BufferedReader(
-                    new InputStreamReader(
-                        connection.getInputStream(), "UTF-8"))) {
-
-                final StringBuilder sb = new StringBuilder();
-                String line;
-
-                while ((line = in.readLine()) != null) {
-                    sb.append(line);
-                    sb.append('\n');
-                }
-
-                return sb.toString();
-            }
-        } catch ( final IOException ioe ) {
-            SystemLogger.error("Unable to read " + name, ioe);
-        }
-        return null;
-    }
-
-    public static File extractFile(final Bundle bundle, final String pid, final String path)
{
-        final URL url = bundle.getEntry(path);
-        if ( url == null ) {
-            SystemLogger.error("Entry " + path + " not found in bundle " + bundle);
-            return null;
-        }
-        URLConnection connection = null;
-        try {
-            connection = url.openConnection();
-
-            final File dir = new File(binDirectory, URLEncoder.encode(pid, "UTF-8"));
-            dir.mkdir();
-            final File newFile = new File(dir, UUID.randomUUID().toString());
-
-            try(final BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
-                final FileOutputStream fos = new FileOutputStream(newFile)) {
-
-                int len = 0;
-                final byte[] buffer = new byte[16384];
-
-                while ( (len = in.read(buffer)) > 0 ) {
-                    fos.write(buffer, 0, len);
-                }
-            }
-
-            return newFile;
-        } catch ( final IOException ioe ) {
-            SystemLogger.error("Unable to read " + path +
-                    " in bundle " + bundle +
-                    " for pid " + pid +
-                    " and write to " + binDirectory, ioe);
-        }
-
-        return null;
-    }
 }

Added: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/BinUtil.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/BinUtil.java?rev=1796661&view=auto
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/BinUtil.java
(added)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/BinUtil.java
Mon May 29 16:17:02 2017
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.configurator.impl.conversion;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLEncoder;
+import java.util.UUID;
+
+import org.osgi.framework.Bundle;
+
+public class BinUtil {
+
+    public static volatile File binDirectory;
+
+    public static File extractFile(final Bundle bundle, final String pid, final String path)
+    throws IOException {
+        final URL url = bundle.getEntry(path);
+        if ( url == null ) {
+            return null;
+        }
+        final URLConnection connection = url.openConnection();
+
+        final File dir = new File(binDirectory, URLEncoder.encode(pid, "UTF-8"));
+        dir.mkdir();
+        final File newFile = new File(dir, UUID.randomUUID().toString());
+
+        try(final BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
+            final FileOutputStream fos = new FileOutputStream(newFile)) {
+
+            int len = 0;
+            final byte[] buffer = new byte[16384];
+
+            while ( (len = in.read(buffer)) > 0 ) {
+                fos.write(buffer, 0, len);
+            }
+        }
+
+        return newFile;
+    }
+}

Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/BinUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/BinUtil.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Copied: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/TypeConverter.java
(from r1796660, felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/TypeConverter.java)
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/TypeConverter.java?p2=felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/TypeConverter.java&p1=felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/TypeConverter.java&r1=1796660&r2=1796661&rev=1796661&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/TypeConverter.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/conversion/TypeConverter.java
Mon May 29 16:17:02 2017
@@ -16,13 +16,14 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.felix.configurator.impl;
+package org.apache.felix.configurator.impl.conversion;
 
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.felix.configurator.impl.logger.SystemLogger;
 import org.osgi.framework.Bundle;
 import org.osgi.util.converter.Converter;
 import org.osgi.util.converter.StandardConverter;
@@ -30,13 +31,14 @@ import org.osgi.util.converter.TypeRefer
 
 public class TypeConverter {
 
+
     public static Converter getConverter() {
         return new StandardConverter();
     }
 
-    private final List<File> allFiles = new ArrayList<File>();
+    private final List<File> allFiles = new ArrayList<>();
 
-    private final List<File> files = new ArrayList<File>();
+    private final List<File> files = new ArrayList<>();
 
     private final Bundle bundle;
 
@@ -102,13 +104,22 @@ public class TypeConverter {
             if ( path == null ) {
                 throw new IOException("Invalid path for binary property: " + value);
             }
-            final File filePath = Util.extractFile(bundle, pid, path);
-            if ( filePath == null ) {
+            try {
+                final File filePath = BinUtil.extractFile(bundle, pid, path);
+                if ( filePath == null ) {
+                    SystemLogger.error("Entry " + path + " not found in bundle " + bundle);
+                    throw new IOException("Invalid path for binary property: " + value);
+                }
+                files.add(filePath);
+                allFiles.add(filePath);
+                return filePath.getAbsolutePath();
+            } catch ( final IOException ioe ) {
+                SystemLogger.error("Unable to read " + path +
+                        " in bundle " + bundle +
+                        " for pid " + pid +
+                        " and write to " + BinUtil.binDirectory, ioe);
                 throw new IOException("Invalid path for binary property: " + value);
             }
-            files.add(filePath);
-            allFiles.add(filePath);
-            return filePath.getAbsolutePath();
 
         } else if ( "binary[]".equals(typeInfo) ) {
             if ( bundle == null ) {
@@ -121,13 +132,22 @@ public class TypeConverter {
             final String[] filePaths = new String[paths.length];
             int i = 0;
             while ( i < paths.length ) {
-                final File filePath = Util.extractFile(bundle, pid, paths[i]);
-                if ( filePath == null ) {
+                try {
+                    final File filePath = BinUtil.extractFile(bundle, pid, paths[i]);
+                    if ( filePath == null ) {
+                        SystemLogger.error("Entry " + paths[i] + " not found in bundle "
+ bundle);
+                        throw new IOException("Invalid path for binary property: " + value);
+                    }
+                    files.add(filePath);
+                    allFiles.add(filePath);
+                    filePaths[i] = filePath.getAbsolutePath();
+                } catch ( final IOException ioe ) {
+                    SystemLogger.error("Unable to read " + paths[i] +
+                            " in bundle " + bundle +
+                            " for pid " + pid +
+                            " and write to " + BinUtil.binDirectory, ioe);
                     throw new IOException("Invalid path for binary property: " + value);
                 }
-                files.add(filePath);
-                allFiles.add(filePath);
-                filePaths[i] = filePath.getAbsolutePath();
                 i++;
             }
             return filePaths;

Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java?rev=1796661&r1=1796660&r2=1796661&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/json/JSONUtil.java
Mon May 29 16:17:02 2017
@@ -18,12 +18,15 @@
  */
 package org.apache.felix.configurator.impl.json;
 
+import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.net.URL;
+import java.net.URLConnection;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -46,8 +49,7 @@ import javax.json.JsonStructure;
 import javax.json.JsonValue;
 import javax.json.JsonValue.ValueType;
 
-import org.apache.felix.configurator.impl.TypeConverter;
-import org.apache.felix.configurator.impl.Util;
+import org.apache.felix.configurator.impl.conversion.TypeConverter;
 import org.apache.felix.configurator.impl.model.BundleState;
 import org.apache.felix.configurator.impl.model.Config;
 import org.apache.felix.configurator.impl.model.ConfigPolicy;
@@ -116,8 +118,8 @@ public class JSONUtil {
                 final int pos = filePath.lastIndexOf('/');
                 final String name = path + filePath.substring(pos);
 
-                final String contents = Util.getResource(name, url);
-                if ( contents != null ) {
+                try {
+                    final String contents = getResource(name, url);
                     boolean done = false;
                     final TypeConverter converter = new TypeConverter(bundle);
                     try {
@@ -131,6 +133,8 @@ public class JSONUtil {
                             converter.cleanupFiles();
                         }
                     }
+                } catch ( final IOException ioe ) {
+                    report.errors.add("Unable to read " + name + " : " + ioe.getMessage());
                 }
             }
             Collections.sort(result);
@@ -391,4 +395,31 @@ public class JSONUtil {
         }
         return (Map<String, ?>) configs;
     }
+
+    /**
+     * Read the contents of a resource, encoded as UTF-8
+     * @param name The resource name
+     * @param url The resource URL
+     * @return The contents
+     * @throws IOException If anything goes wrong
+     */
+    public static String getResource(final String name, final URL url)
+    throws IOException {
+        final URLConnection connection = url.openConnection();
+
+        try(final BufferedReader in = new BufferedReader(
+                new InputStreamReader(
+                    connection.getInputStream(), "UTF-8"))) {
+
+            final StringBuilder sb = new StringBuilder();
+            String line;
+
+            while ((line = in.readLine()) != null) {
+                sb.append(line);
+                sb.append('\n');
+            }
+
+            return sb.toString();
+        }
+    }
 }

Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/AbstractState.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/AbstractState.java?rev=1796661&r1=1796660&r2=1796661&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/AbstractState.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/AbstractState.java
Mon May 29 16:17:02 2017
@@ -24,8 +24,6 @@ import java.util.Collection;
 import java.util.Map;
 import java.util.TreeMap;
 
-import org.apache.felix.configurator.impl.Util;
-
 /**
  * This object holds a sorted map of configurations
  */
@@ -62,7 +60,7 @@ public class AbstractState implements Se
         if ( version < 1 || version > VERSION ) {
             throw new ClassNotFoundException(this.getClass().getName());
         }
-        Util.setField(this, "configurationsByPid", in.readObject());
+        ReflectionUtil.setField(this, "configurationsByPid", in.readObject());
     }
 
     public void add(final Config c) {

Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/Config.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/Config.java?rev=1796661&r1=1796660&r2=1796661&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/Config.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/Config.java
Mon May 29 16:17:02 2017
@@ -25,8 +25,6 @@ import java.util.Dictionary;
 import java.util.List;
 import java.util.Set;
 
-import org.apache.felix.configurator.impl.Util;
-
 public class Config implements Serializable, Comparable<Config> {
 
     private static final long serialVersionUID = 1L;
@@ -107,12 +105,12 @@ public class Config implements Serializa
         if ( version < 1 || version > VERSION ) {
             throw new ClassNotFoundException(this.getClass().getName());
         }
-        Util.setField(this, "pid", in.readObject());
-        Util.setField(this, "properties", in.readObject());
-        Util.setField(this, "environments", in.readObject());
-        Util.setField(this, "policy", ConfigPolicy.valueOf((String)in.readObject()));
-        Util.setField(this, "bundleId", in.readLong());
-        Util.setField(this, "ranking", in.readInt());
+        ReflectionUtil.setField(this, "pid", in.readObject());
+        ReflectionUtil.setField(this, "properties", in.readObject());
+        ReflectionUtil.setField(this, "environments", in.readObject());
+        ReflectionUtil.setField(this, "policy", ConfigPolicy.valueOf((String)in.readObject()));
+        ReflectionUtil.setField(this, "bundleId", in.readLong());
+        ReflectionUtil.setField(this, "ranking", in.readInt());
         this.index = in.readInt();
         this.state = ConfigState.valueOf((String)in.readObject());
         this.files = (List<File>) in.readObject();

Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ConfigList.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ConfigList.java?rev=1796661&r1=1796660&r2=1796661&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ConfigList.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ConfigList.java
Mon May 29 16:17:02 2017
@@ -25,8 +25,6 @@ import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
-import org.apache.felix.configurator.impl.Util;
-
 /**
  * The config list holds all configurations for a single PID
  */
@@ -75,7 +73,7 @@ public class ConfigList implements Seria
         if ( version < 1 || version > VERSION ) {
             throw new ClassNotFoundException(this.getClass().getName());
         }
-        Util.setField(this, "configurations", in.readObject());
+        ReflectionUtil.setField(this, "configurations", in.readObject());
         lastInstalled = (Config) in.readObject();
         this.changeCount = in.readLong();
         this.hasChanges = in.readBoolean();

Added: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ReflectionUtil.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ReflectionUtil.java?rev=1796661&view=auto
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ReflectionUtil.java
(added)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ReflectionUtil.java
Mon May 29 16:17:02 2017
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.configurator.impl.model;
+
+import java.io.IOException;
+import java.lang.reflect.Field;
+
+public class ReflectionUtil {
+
+    /**
+     * Set a (final) field during deserialization.
+     */
+    public static void setField(final Object obj, final String name, final Object value)
+    throws IOException {
+        Class<?> clazz = obj.getClass();
+        while ( clazz != null ) {
+            try {
+                final Field field = clazz.getDeclaredField(name);
+                field.setAccessible(true);
+                field.set(obj, value);
+                return;
+            } catch (final NoSuchFieldException e) {
+                clazz = clazz.getSuperclass();
+            } catch (final SecurityException | IllegalArgumentException  | IllegalAccessException
e) {
+                throw (IOException)new IOException().initCause(e);
+            }
+        }
+        throw new IOException("Field " + name + " not found in class " + obj.getClass());
+    }
+
+}

Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ReflectionUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/ReflectionUtil.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/State.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/State.java?rev=1796661&r1=1796660&r2=1796661&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/State.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/model/State.java
Mon May 29 16:17:02 2017
@@ -30,8 +30,6 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.felix.configurator.impl.Util;
-import org.apache.felix.configurator.impl.logger.SystemLogger;
 import org.osgi.framework.BundleContext;
 
 public class State extends AbstractState implements Serializable {
@@ -41,7 +39,7 @@ public class State extends AbstractState
     /** Serialization version. */
     private static final int VERSION = 1;
 
-    private static final String FILE_NAME = "state.ser";
+    public static final String FILE_NAME = "state.ser";
 
     private final Map<Long, Long> bundlesLastModified = new HashMap<>();
 
@@ -80,12 +78,13 @@ public class State extends AbstractState
         if ( version < 1 || version > VERSION ) {
             throw new ClassNotFoundException(this.getClass().getName());
         }
-        Util.setField(this, "bundlesLastModified", in.readObject());
-        Util.setField(this, "environments", in.readObject());
+        ReflectionUtil.setField(this, "bundlesLastModified", in.readObject());
+        ReflectionUtil.setField(this, "environments", in.readObject());
         initialHashes = (Set<String>) in.readObject();
     }
 
-    public static State createOrReadState(final BundleContext bc) {
+    public static State createOrReadState(final BundleContext bc)
+    throws ClassNotFoundException, IOException {
         final File f = bc.getDataFile(FILE_NAME);
         if ( f == null || !f.exists() ) {
             return new State();
@@ -93,13 +92,11 @@ public class State extends AbstractState
         try ( final ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))
) {
 
             return (State) ois.readObject();
-        } catch ( final ClassNotFoundException | IOException e ) {
-            SystemLogger.error("Unable to read persisted state from " + f, e);
-            return new State();
         }
     }
 
-    public static void writeState(final BundleContext bc, final State state) {
+    public static void writeState(final BundleContext bc, final State state)
+    throws IOException {
         final File f = bc.getDataFile(FILE_NAME);
         if ( f == null ) {
             // do nothing, no file system support
@@ -107,8 +104,6 @@ public class State extends AbstractState
         }
         try ( final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f))
) {
             oos.writeObject(state);
-        } catch ( final IOException e) {
-            SystemLogger.error("Unable to persist state to " + f, e);
         }
     }
 

Copied: felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/conversion/TypeConverterTest.java
(from r1796660, felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/TypeConverterTest.java)
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/conversion/TypeConverterTest.java?p2=felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/conversion/TypeConverterTest.java&p1=felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/TypeConverterTest.java&r1=1796660&r2=1796661&rev=1796661&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/TypeConverterTest.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/conversion/TypeConverterTest.java
Mon May 29 16:17:02 2017
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.felix.configurator.impl;
+package org.apache.felix.configurator.impl.conversion;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -29,6 +29,7 @@ import java.util.Collection;
 
 import javax.json.JsonObject;
 
+import org.apache.felix.configurator.impl.conversion.TypeConverter;
 import org.apache.felix.configurator.impl.json.JSONUtil;
 import org.apache.felix.configurator.impl.json.JSONUtilTest;
 import org.junit.Test;

Modified: felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/json/JSONUtilTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/json/JSONUtilTest.java?rev=1796661&r1=1796660&r2=1796661&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/json/JSONUtilTest.java
(original)
+++ felix/trunk/osgi-r7/configurator/src/test/java/org/apache/felix/configurator/impl/json/JSONUtilTest.java
Mon May 29 16:17:02 2017
@@ -31,7 +31,7 @@ import java.util.List;
 
 import javax.json.JsonObject;
 
-import org.apache.felix.configurator.impl.TypeConverter;
+import org.apache.felix.configurator.impl.conversion.TypeConverter;
 import org.apache.felix.configurator.impl.model.ConfigurationFile;
 import org.junit.Test;
 



Mime
View raw message