You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by ff...@apache.org on 2014/06/27 06:41:46 UTC

git commit: [KARAF-3075]AdminService should avoid writing file in case just read instance status

Repository: karaf
Updated Branches:
  refs/heads/karaf-2.x a05213c12 -> 32f47f4c2


[KARAF-3075]AdminService should avoid writing file in case just read instance status


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/32f47f4c
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/32f47f4c
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/32f47f4c

Branch: refs/heads/karaf-2.x
Commit: 32f47f4c22c2a520e9ea8cd97b70c1a776c5da6c
Parents: a05213c
Author: Freeman Fang <fr...@gmail.com>
Authored: Fri Jun 27 12:41:24 2014 +0800
Committer: Freeman Fang <fr...@gmail.com>
Committed: Fri Jun 27 12:41:24 2014 +0800

----------------------------------------------------------------------
 .../karaf/admin/internal/AdminServiceImpl.java  | 46 ++++++++++----------
 .../main/java/org/apache/karaf/main/Main.java   |  2 +-
 .../karaf/util/properties/FileLockUtils.java    | 25 ++++++-----
 3 files changed, 40 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/32f47f4c/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java
----------------------------------------------------------------------
diff --git a/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java b/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java
index 3fa2430..9e0c7d5 100644
--- a/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java
+++ b/admin/core/src/main/java/org/apache/karaf/admin/internal/AdminServiceImpl.java
@@ -199,7 +199,7 @@ public class AdminServiceImpl implements AdminService {
         T call(State state) throws IOException;
     }
 
-    synchronized <T> T execute(final Task<T> callback) {
+    synchronized <T> T execute(final Task<T> callback, final boolean writeToFile) {
         final File storageFile = new File(storageLocation, STORAGE_FILE);
         if (!storageFile.exists()) {
             storageFile.getParentFile().mkdirs();
@@ -218,10 +218,12 @@ public class AdminServiceImpl implements AdminService {
                     public T call(org.apache.felix.utils.properties.Properties properties) throws IOException {
                         State state = loadData(properties);
                         T t = callback.call(state);
-                        saveData(state, properties);
+                        if (writeToFile) {
+                            saveData(state, properties);
+                        }
                         return t;
                     }
-                });
+                }, writeToFile);
             } catch (IOException e) {
                 throw new RuntimeException(e);
             }
@@ -332,7 +334,7 @@ public class AdminServiceImpl implements AdminService {
                 AdminServiceImpl.this.proxies.put(name, instance);
                 return instance;
             }
-        });
+        }, true);
     }
 
     void handleFeatures(File file, final InstanceSettings settings) throws IOException {
@@ -341,7 +343,7 @@ public class AdminServiceImpl implements AdminService {
                 appendToPropList(properties, "featuresBoot", settings.getFeatures());
                 appendToPropList(properties, "featuresRepositories", settings.getFeatureURLs());
             }
-        });
+        }, true);
     }
 
     private void appendToPropList(org.apache.felix.utils.properties.Properties p, String key, List<String> elements) {
@@ -363,7 +365,7 @@ public class AdminServiceImpl implements AdminService {
             public Instance[] call(State state) throws IOException {
                 return proxies.values().toArray(new Instance[proxies.size()]);
             }
-        });
+        }, false);
     }
 
     public Instance getInstance(final String name) {
@@ -371,7 +373,7 @@ public class AdminServiceImpl implements AdminService {
             public Instance call(State state) throws IOException {
                 return proxies.get(name);
             }
-        });
+        }, false);
     }
 
     public void startInstance(final String name, final String javaOpts) {
@@ -449,7 +451,7 @@ public class AdminServiceImpl implements AdminService {
                 }
                 return classpath;
             }
-        });
+        }, true);
     }
 
     public void stopInstance(final String name) {
@@ -470,7 +472,7 @@ public class AdminServiceImpl implements AdminService {
                 }
                 return null;
             }
-        });
+        }, true);
     }
 
     public void destroyInstance(final String name) {
@@ -489,7 +491,7 @@ public class AdminServiceImpl implements AdminService {
                 AdminServiceImpl.this.proxies.remove(name);
                 return null;
             }
-        });
+        }, true);
     }
 
     public void renameInstance(final String oldName, final String newName) throws Exception {
@@ -549,7 +551,7 @@ public class AdminServiceImpl implements AdminService {
                 AdminServiceImpl.this.proxies.put(newName, proxy);
                 return null;
             }
-        });
+        }, true);
     }
 
     public synchronized Instance cloneInstance(final String name, final String cloneName, final InstanceSettings settings) throws Exception {
@@ -614,7 +616,7 @@ public class AdminServiceImpl implements AdminService {
                 AdminServiceImpl.this.proxies.put(cloneName, cloneInstance);
                 return cloneInstance;
             }
-        });
+        }, true);
     }
 
     private void checkPid(InstanceState instance) throws IOException {
@@ -695,7 +697,7 @@ public class AdminServiceImpl implements AdminService {
             public Integer call(State state) throws IOException {
                 return AdminServiceImpl.this.getKarafPort(state, name, path, key);
             }
-        });
+        }, false);
     }
 
     private Integer getKarafPort(State state, String name, String path, final String key) {
@@ -709,7 +711,7 @@ public class AdminServiceImpl implements AdminService {
                 public Integer call(org.apache.felix.utils.properties.Properties properties) throws IOException {
                     return Integer.parseInt(properties.get(key).toString());
                 }
-            });
+            }, false);
         } catch (IOException e) {
             return 0;
         }
@@ -731,10 +733,10 @@ public class AdminServiceImpl implements AdminService {
                     public void run(org.apache.felix.utils.properties.Properties properties) throws IOException {
                         properties.put(key, Integer.toString(port));
                     }
-                });
+                }, true);
                 return null;
             }
-        });
+        }, true);
     }
 
     boolean isInstanceRoot(final String name) {
@@ -746,7 +748,7 @@ public class AdminServiceImpl implements AdminService {
                 }
                 return instance.root;
             }
-        });
+        }, false);
     }
 
     String getInstanceLocation(final String name) {
@@ -758,7 +760,7 @@ public class AdminServiceImpl implements AdminService {
                 }
                 return instance.loc;
             }
-        });
+        }, true);
     }
 
     int getInstancePid(final String name) {
@@ -771,7 +773,7 @@ public class AdminServiceImpl implements AdminService {
                 checkPid(instance);
                 return instance.pid;
             }
-        });
+        }, false);
     }
 
     String getInstanceJavaOpts(final String name) {
@@ -783,7 +785,7 @@ public class AdminServiceImpl implements AdminService {
                 }
                 return instance.opts;
             }
-        });
+        }, false);
     }
 
     void changeInstanceJavaOpts(final String name, final String opts) {
@@ -796,7 +798,7 @@ public class AdminServiceImpl implements AdminService {
                 instance.opts = opts;
                 return null;
             }
-        });
+        }, true);
     }
 
     String getInstanceState(final String name) {
@@ -824,7 +826,7 @@ public class AdminServiceImpl implements AdminService {
                     return Instance.STARTING;
                 }
             }
-        });
+        }, true);
     }
 
     private boolean deleteFile(File fileToDelete) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/32f47f4c/main/src/main/java/org/apache/karaf/main/Main.java
----------------------------------------------------------------------
diff --git a/main/src/main/java/org/apache/karaf/main/Main.java b/main/src/main/java/org/apache/karaf/main/Main.java
index 784b6fd..ece7b3a 100644
--- a/main/src/main/java/org/apache/karaf/main/Main.java
+++ b/main/src/main/java/org/apache/karaf/main/Main.java
@@ -674,7 +674,7 @@ public class Main {
                             }
                         }
                     }
-                });
+                }, true);
             }
         } catch (Exception e) {
             System.err.println("Unable to update instance pid: " + e.getMessage());

http://git-wip-us.apache.org/repos/asf/karaf/blob/32f47f4c/util/src/main/java/org/apache/karaf/util/properties/FileLockUtils.java
----------------------------------------------------------------------
diff --git a/util/src/main/java/org/apache/karaf/util/properties/FileLockUtils.java b/util/src/main/java/org/apache/karaf/util/properties/FileLockUtils.java
index 1e72e34..d01f9cb 100644
--- a/util/src/main/java/org/apache/karaf/util/properties/FileLockUtils.java
+++ b/util/src/main/java/org/apache/karaf/util/properties/FileLockUtils.java
@@ -64,6 +64,7 @@ public final class FileLockUtils {
         RandomAccessFile raf = new RandomAccessFile(file, "rw");
         try {
             FileLock lock = raf.getChannel().lock();
+            
             try {
                 return callback.call(raf);
             } finally {
@@ -74,7 +75,7 @@ public final class FileLockUtils {
         }
     }
 
-    public static void execute(File file, final RunnableWithProperties callback) throws IOException {
+    public static void execute(File file, final RunnableWithProperties callback, final boolean writeToFile) throws IOException {
         execute(file, new Runnable() {
             public void run(RandomAccessFile file) throws IOException {
                 byte[] buffer = new byte[(int) file.length()];
@@ -82,15 +83,17 @@ public final class FileLockUtils {
                 Properties props = new Properties();
                 props.load(new ByteArrayInputStream(buffer));
                 callback.run(props);
-                ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                props.store(baos, null);
-                file.setLength(0);
-                file.write(baos.toByteArray());
+                if (writeToFile) {
+                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                    props.store(baos, null);
+                    file.setLength(0);
+                    file.write(baos.toByteArray());
+                }
             }
         });
     }
 
-    public static <T> T execute(File file, final CallableWithProperties<T> callback) throws IOException {
+    public static <T> T execute(File file, final CallableWithProperties<T> callback, final boolean writeToFile) throws IOException {
         return execute(file, new Callable<T>() {
             public T call(RandomAccessFile file) throws IOException {
                 byte[] buffer = new byte[(int) file.length()];
@@ -98,10 +101,12 @@ public final class FileLockUtils {
                 Properties props = new Properties();
                 props.load(new ByteArrayInputStream(buffer));
                 T result = callback.call(props);
-                ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                props.store(baos, null);
-                file.setLength(0);
-                file.write(baos.toByteArray());
+                if (writeToFile) {
+                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                    props.store(baos, null);
+                    file.setLength(0);
+                    file.write(baos.toByteArray());
+                }
                 return result;
             }
         });