Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,27 @@
package org.apache.felix.fileinstall.internal;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.nio.file.Files;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
Expand Down Expand Up @@ -257,46 +262,83 @@ public void doConfigurationEvent(ConfigurationEvent configurationEvent)
return;
}

TypedProperties props = new TypedProperties( bundleSubstitution() );
try (Reader r = new InputStreamReader(new FileInputStream(file), encoding()))
if (fileInstall.lock.getReadHoldCount() > 0)
{
props.load(r);
Util.log(context, Logger.LOG_WARNING, "Cannot save configuration because thread holds read lock: " + fileName, null);
return;
}
// remove "removed" properties from the cfg file
List<String> propertiesToRemove = new ArrayList<>();
for( String key : props.keySet() )

// Guard load-merge-compare-write as one critical section so DirectoryWatcher cannot
// pick up a partially written (empty) file between the truncate and the write.
// The read lock check above is a safety net: DirectoryWatcher holds the read lock
// while processing files, and ReentrantReadWriteLock does not allow upgrading a
// read lock to a write lock on the same thread.
fileInstall.lock.writeLock().lockInterruptibly();
try
{
if( dict.get(key) == null
&& !Constants.SERVICE_PID.equals(key)
&& !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key)
&& !DirectoryWatcher.FILENAME.equals(key) ) {
propertiesToRemove.add(key);
byte[] currentContent = Files.readAllBytes(file.toPath());
TypedProperties props = new TypedProperties( bundleSubstitution() );
try (Reader r = new InputStreamReader(new ByteArrayInputStream(currentContent), encoding()))
{
props.load(r);
}
}
for( Enumeration<?> e = dict.keys(); e.hasMoreElements(); )
{
String key = e.nextElement().toString();
if( !Constants.SERVICE_PID.equals(key)
&& !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key)
&& !DirectoryWatcher.FILENAME.equals(key) )
// remove "removed" properties from the cfg file
List<String> propertiesToRemove = new ArrayList<>();
for( String key : props.keySet() )
{
Object val = dict.get( key );
props.put( key, val );
if( dict.get(key) == null
&& !Constants.SERVICE_PID.equals(key)
&& !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key)
&& !DirectoryWatcher.FILENAME.equals(key) ) {
propertiesToRemove.add(key);
}
}
for( Enumeration<?> e = dict.keys(); e.hasMoreElements(); )
{
String key = e.nextElement().toString();
if( !Constants.SERVICE_PID.equals(key)
&& !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key)
&& !DirectoryWatcher.FILENAME.equals(key) )
{
Object val = dict.get( key );
props.put( key, val );
}
}
for( String key : propertiesToRemove )
{
props.remove(key);
}

ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (Writer fw = new OutputStreamWriter(baos, encoding()))
{
props.save( fw );
}
byte[] newContent = baos.toByteArray();

// do not touch the file (and its lastModified) if there is nothing to change
if (!Arrays.equals(newContent, currentContent))
{
try (OutputStream os = new FileOutputStream(file))
{
os.write(newContent);
}
// we're just writing out what's already loaded into ConfigAdmin, so
// update file checksum since lastModified gets updated when writing
fileInstall.updateChecksum(file);
}
}
for( String key : propertiesToRemove )
{
props.remove(key);
}
try (Writer fw = new OutputStreamWriter(new FileOutputStream(file), encoding()))
finally
{
props.save( fw );
fileInstall.lock.writeLock().unlock();
}
// we're just writing out what's already loaded into ConfigAdmin, so
// update file checksum since lastModified gets updated when writing
fileInstall.updateChecksum(file);
}
}
catch (InterruptedException e)
{
Thread.currentThread().interrupt();
Util.log( context, Logger.LOG_ERROR, "Unable to save configuration", e );
}
catch (Exception e)
{
Util.log( context, Logger.LOG_ERROR, "Unable to save configuration", e );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.File;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.felix.fileinstall.ArtifactInstaller;
Expand Down Expand Up @@ -60,7 +59,7 @@ public class FileInstall implements BundleActivator, ServiceTrackerCustomizer
BundleContext context;
final Map<String, DirectoryWatcher> watchers = new HashMap<String, DirectoryWatcher>();
ServiceTracker listenersTracker;
final ReadWriteLock lock = new ReentrantReadWriteLock();
final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
ServiceRegistration urlHandlerRegistration;
volatile boolean stopped;

Expand Down
Loading
Loading