Skip to content

Commit

Permalink
[KARAF-5178] Use lambdas where appropriate
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Kitt <[email protected]>
  • Loading branch information
skitt committed Jun 2, 2017
1 parent 928a249 commit bf66663
Show file tree
Hide file tree
Showing 58 changed files with 856 additions and 1,380 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,8 @@ protected void formatClause(Clause clause, StringBuilder builder, int indent) {
String name = clause.getName();
Directive[] directives = clause.getDirectives();
Attribute[] attributes = clause.getAttributes();
Arrays.sort(directives, new Comparator<Directive>() {
public int compare(Directive o1, Directive o2) {
return o1.getName().compareTo(o2.getName());
}
});
Arrays.sort(attributes, new Comparator<Attribute>() {
public int compare(Attribute o1, Attribute o2) {
return o1.getName().compareTo(o2.getName());
}
});
Arrays.sort(directives, Comparator.comparing(Directive::getName));
Arrays.sort(attributes, Comparator.comparing(Attribute::getName));
builder.append(name);
for (int i = 0; directives != null && i < directives.length; i++) {
if (noUses && directives[i].getName().equalsIgnoreCase("uses")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.wiring.FrameworkWiring;

Expand Down Expand Up @@ -87,93 +86,85 @@ public Object execute() throws Exception {
locks[b].set(false);
}
for (int i = 0; i < threads; i++) {
new Thread() {
public void run() {
try {
Random rand = new Random();
for (int j = 0; j < iterations; j++) {
for (;;) {
int b = rand.nextInt(bundles.length);
if (locks[b].compareAndSet(false, true)) {
try {
// Only touch active bundles
if (bundles[b].getState() != Bundle.ACTIVE) {
continue;
}
if (rand.nextInt(100) < refresh) {
try {
bundles[b].update();
final CountDownLatch latch = new CountDownLatch(1);
wiring.refreshBundles(Collections.singletonList(bundles[b]), new FrameworkListener() {
public void frameworkEvent(FrameworkEvent event) {
latch.countDown();
}
});
latch.await();
} finally {
while (true) {
try {
bundles[b].start(Bundle.START_TRANSIENT);
break;
} catch (Exception e) {
Thread.sleep(1);
}
}
}
} else {
try {
bundles[b].stop(Bundle.STOP_TRANSIENT);
} finally {
while (true) {
try {
bundles[b].start(Bundle.START_TRANSIENT);
break;
} catch (Exception e) {
Thread.sleep(1);
}
new Thread(() -> {
try {
Random rand = new Random();
for (int j = 0; j < iterations; j++) {
for (;;) {
int b = rand.nextInt(bundles.length);
if (locks[b].compareAndSet(false, true)) {
try {
// Only touch active bundles
if (bundles[b].getState() != Bundle.ACTIVE) {
continue;
}
if (rand.nextInt(100) < refresh) {
try {
bundles[b].update();
final CountDownLatch latch1 = new CountDownLatch(1);
wiring.refreshBundles(Collections.singletonList(bundles[b]),
(FrameworkListener) event -> latch1.countDown());
latch1.await();
} finally {
while (true) {
try {
bundles[b].start(Bundle.START_TRANSIENT);
break;
} catch (Exception e) {
Thread.sleep(1);
}
}
}
Thread.sleep(rand.nextInt(delay));
} catch (Exception e) {
boolean ignore = false;
if (e instanceof BundleException && e.getMessage() != null) {
String msg = e.getMessage();
if ("Cannot acquire global lock to update the bundle.".equals(msg) ||
"Unable to acquire global lock for resolve.".equals(msg) ||
msg.matches("Bundle .* cannot be update, since it is either starting or stopping.")) {
ignore = true;
} else {
try {
bundles[b].stop(Bundle.STOP_TRANSIENT);
} finally {
while (true) {
try {
bundles[b].start(Bundle.START_TRANSIENT);
break;
} catch (Exception e) {
Thread.sleep(1);
}
}
}
if (!ignore) {
e.printStackTrace();
}
Thread.sleep(rand.nextInt(delay));
} catch (Exception e) {
boolean ignore = false;
if (e instanceof BundleException && e.getMessage() != null) {
String msg = e.getMessage();
if ("Cannot acquire global lock to update the bundle.".equals(msg) ||
"Unable to acquire global lock for resolve.".equals(msg) ||
msg.matches("Bundle .* cannot be update, since it is either starting or stopping.")) {
ignore = true;
}
} finally {
locks[b].set(false);
}
if (!ignore) {
e.printStackTrace();
}
} finally {
locks[b].set(false);
}
break;
}
break;
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
latch.countDown();
}
} catch (Throwable t) {
t.printStackTrace();
} finally {
latch.countDown();
}
}.start();
}).start();
}
new Thread() {
@Override
public void run() {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("Load test finished");
new Thread(() -> {
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}.start();
System.err.println("Load test finished");
}).start();
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,16 @@ private void printHeader(Bundle bundle) {
*/
private void printTree(Tree<Bundle> tree) {
System.out.printf("%n");
tree.write(System.out, new Tree.Converter<Bundle>() {

public String toString(Node<Bundle> node) {
if (versions) {
return String.format("%s / [%s] [%s]",
node.getValue().getSymbolicName(),
node.getValue().getVersion().toString(),
node.getValue().getBundleId());
} else {
return String.format("%s [%s]",
node.getValue().getSymbolicName(),
node.getValue().getBundleId());
}
tree.write(System.out, node -> {
if (versions) {
return String.format("%s / [%s] [%s]",
node.getValue().getSymbolicName(),
node.getValue().getVersion().toString(),
node.getValue().getBundleId());
} else {
return String.format("%s [%s]",
node.getValue().getSymbolicName(),
node.getValue().getBundleId());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ public void write(PrintStream stream, Converter<T> converter) {
* @param writer the writer where to write.
*/
public void write(PrintWriter writer) {
write(writer, new Converter<T>() {
public String toString(Node<T> node) {
return node.getValue().toString();
}
});
write(writer, node -> node.getValue().toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ public void run() {
if (!updated.isEmpty()) {
try {
final CountDownLatch latch = new CountDownLatch(1);
wiring.refreshBundles(updated, new FrameworkListener() {
public void frameworkEvent(FrameworkEvent event) {
latch.countDown();
}
});
wiring.refreshBundles(updated, (FrameworkListener) event -> latch.countDown());
latch.await();
} catch (InterruptedException e) {
running.set(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
import java.io.StringWriter;
import java.util.Set;

import org.apache.karaf.bundle.command.bundletree.Node;
import org.apache.karaf.bundle.command.bundletree.Tree;
import org.junit.Test;

/**
* Test cases for {@link org.apache.karaf.shell.dev.util.Tree}
* and {@link org.apache.karaf.shell.dev.util.Node}
* Test cases for {@link org.apache.karaf.bundle.command.bundletree.Tree}
* and {@link org.apache.karaf.bundle.command.bundletree.Node}
*/
public class TreeTest {

Expand All @@ -55,11 +53,7 @@ public void writeTreeWithOneChildAndNodeConverter() throws IOException {
tree.addChild("child");

StringWriter writer = new StringWriter();
tree.write(new PrintWriter(writer), new Tree.Converter<String>() {
public String toString(Node<String> node) {
return "my " + node.getValue();
}
});
tree.write(new PrintWriter(writer), node -> "my " + node.getValue());

BufferedReader reader = new BufferedReader(new StringReader(writer.getBuffer().toString()));

Expand Down
21 changes: 7 additions & 14 deletions client/src/main/java/org/apache/karaf/client/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.Reader;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -102,12 +100,9 @@ public static void main(String[] args) throws Exception {
FilePasswordProvider passwordProvider = null;
final Console console = System.console();
if (console != null) {
passwordProvider = new FilePasswordProvider() {
@Override
public String getPassword(String resourceKey) throws IOException {
char[] pwd = console.readPassword("Enter password for " + resourceKey + ": ");
return new String(pwd);
}
passwordProvider = resourceKey -> {
char[] pwd = console.readPassword("Enter password for " + resourceKey + ": ");
return new String(pwd);
};
client.setFilePasswordProvider(passwordProvider);
client.setUserInteraction(new UserInteraction() {
Expand Down Expand Up @@ -337,12 +332,10 @@ private static void registerSignalHandler(final Terminal terminal, final PtyCapa
Class<?> signalHandlerClass = Class.forName("sun.misc.SignalHandler");
// Implement signal handler
Object signalHandler = Proxy.newProxyInstance(Main.class.getClassLoader(),
new Class<?>[]{signalHandlerClass}, new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Size size = terminal.getSize();
channel.sendWindowChange(size.getColumns(), size.getRows());
return null;
}
new Class<?>[]{signalHandlerClass}, (proxy, method, args) -> {
Size size = terminal.getSize();
channel.sendWindowChange(size.getColumns(), size.getRows());
return null;
}
);
// Register the signal handler, this code is equivalent to:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.List;

import org.apache.karaf.config.core.impl.MetaServiceCaller;
import org.apache.karaf.config.core.impl.MetatypeCallable;
import org.apache.karaf.shell.api.action.lifecycle.Init;
import org.apache.karaf.shell.api.action.lifecycle.Reference;
import org.apache.karaf.shell.api.action.lifecycle.Service;
Expand All @@ -34,7 +33,6 @@
import org.osgi.framework.BundleEvent;
import org.osgi.framework.BundleListener;
import org.osgi.service.metatype.MetaTypeInformation;
import org.osgi.service.metatype.MetaTypeService;

@Service
public class MetaCompleter implements Completer, BundleListener {
Expand All @@ -60,27 +58,23 @@ public void bundleChanged(BundleEvent event) {
}

private synchronized void updateMeta() {
List<String> pids = MetaServiceCaller.withMetaTypeService(context, new MetatypeCallable<List<String>>() {
List<String> pids = MetaServiceCaller.withMetaTypeService(context, metatypeService -> {
List<String> pids1 = new ArrayList<>();
Bundle[] bundles = context.getBundles();
for (Bundle bundle : bundles) {

@Override
public List<String> callWith(MetaTypeService metatypeService) {
List<String> pids = new ArrayList<>();
Bundle[] bundles = context.getBundles();
for (Bundle bundle : bundles) {

MetaTypeInformation info = metatypeService.getMetaTypeInformation(bundle);
if (info == null) {
continue;
}
if (info.getFactoryPids() != null) {
pids.addAll(Arrays.asList(info.getFactoryPids()));
}
if (info.getPids() != null) {
pids.addAll(Arrays.asList(info.getPids()));
}
MetaTypeInformation info = metatypeService.getMetaTypeInformation(bundle);
if (info == null) {
continue;
}
if (info.getFactoryPids() != null) {
pids1.addAll(Arrays.asList(info.getFactoryPids()));
}
if (info.getPids() != null) {
pids1.addAll(Arrays.asList(info.getPids()));
}
return pids;
}
return pids1;
});
if (pids != null) {
delegate.getStrings().clear();
Expand Down
Loading

0 comments on commit bf66663

Please sign in to comment.