KARAF-1785 Provide additional actions for SCR plugin

Signed-off-by: Lukasz Dywicki <luke@code-house.org>

git-svn-id: https://svn.apache.org/repos/asf/karaf/webconsole/trunk@1379106 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/osgi/scr/pom.xml b/osgi/scr/pom.xml
index 7b2da6b..c7d07eb 100644
--- a/osgi/scr/pom.xml
+++ b/osgi/scr/pom.xml
@@ -61,7 +61,7 @@
                         </Import-Package>
                         <Export-Package></Export-Package>
                         <Private-Package>org.apache.karaf.webconsole.osgi.scr*,</Private-Package>
-                        <Service-Component>OSGI-INF/column.xml,OSGI-INF/decoration.xml,OSGI-INF/component.xml</Service-Component>
+                        <Service-Component>OSGI-INF/column.xml,OSGI-INF/decoration.xml,OSGI-INF/action.xml,OSGI-INF/component.xml</Service-Component>
                     </instructions>
                 </configuration>
             </plugin>
diff --git a/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/ScrActionPanel.java b/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/ScrActionPanel.java
new file mode 100644
index 0000000..b86574f
--- /dev/null
+++ b/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/ScrActionPanel.java
@@ -0,0 +1,82 @@
+/*
+ * 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.karaf.webconsole.osgi.scr;
+
+import org.apache.felix.scr.Component;
+import org.apache.karaf.webconsole.osgi.scr.link.DisableLink;
+import org.apache.karaf.webconsole.osgi.scr.link.EnableLink;
+import org.apache.wicket.markup.html.WebMarkupContainer;
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.link.Link;
+import org.apache.wicket.markup.html.panel.Panel;
+import org.apache.wicket.markup.repeater.RepeatingView;
+import org.apache.wicket.model.IModel;
+import org.osgi.framework.Bundle;
+
+public class ScrActionPanel extends Panel {
+
+    private static final long serialVersionUID = 1L;
+    private RepeatingView view;
+    private int childCount = 0;
+
+    public ScrActionPanel(String componentId, IModel<Bundle> model, Component[] components) {
+        super(componentId, model);
+
+        view = new RepeatingView("components");
+        for (Component component : components) {
+            switch (component.getState()) {
+            case Component.STATE_ACTIVE:
+                view.add(createDisableLink(component, view.newChildId()));
+                childCount++;
+                break;
+            case Component.STATE_DISABLED:
+                view.add(createEnableLink(component, view.newChildId()));
+                childCount++;
+                break;
+            }
+        }
+
+        add(view);
+    }
+
+    private WebMarkupContainer createDisableLink(Component component, String childId) {
+        WebMarkupContainer container = new WebMarkupContainer(childId);
+        Link<Bundle> link = new DisableLink("link", getModel(), component.getName());
+        link.add(new Label("label", "Disable " + component.getName()));
+        container.add(link);
+        return container;
+    }
+
+    private WebMarkupContainer createEnableLink(Component component, String childId) {
+        WebMarkupContainer container = new WebMarkupContainer(childId);
+        Link<Bundle> link = new EnableLink("link", getModel(), component.getName());
+        link.add(new Label("label", "Enable " + component.getName()));
+        container.add(link);
+        return container;
+    }
+
+    @SuppressWarnings("unchecked")
+    public IModel<Bundle> getModel() {
+        return (IModel<Bundle>) getDefaultModel();
+    }
+
+    @Override
+    public boolean isVisible() {
+        return childCount > 0;
+    }
+
+}
diff --git a/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/ScrActionProvider.java b/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/ScrActionProvider.java
new file mode 100644
index 0000000..2672a97
--- /dev/null
+++ b/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/ScrActionProvider.java
@@ -0,0 +1,45 @@
+/*
+ * 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.karaf.webconsole.osgi.scr;
+
+import org.apache.felix.scr.Component;
+import org.apache.felix.scr.ScrService;
+import org.apache.karaf.webconsole.osgi.core.shared.BundleModel;
+import org.apache.karaf.webconsole.osgi.core.spi.IActionProvider;
+import org.apache.wicket.markup.html.panel.EmptyPanel;
+import org.apache.wicket.markup.html.panel.Panel;
+import org.osgi.framework.Bundle;
+
+public class ScrActionProvider implements IActionProvider {
+
+    public Panel create(String componentId, Bundle object) {
+        ScrService scr = ScrComponent.getScrService();
+        if (scr == null) {
+            return null;
+        }
+
+        Component[] components = scr.getComponents(object);
+        if (components != null) {
+            return new ScrActionPanel(componentId, new BundleModel(object), components);
+        }
+
+        EmptyPanel panel = new EmptyPanel(componentId);
+        panel.setVisible(false);
+        return panel;
+    }
+
+}
diff --git a/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/link/DisableLink.java b/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/link/DisableLink.java
new file mode 100644
index 0000000..5c54c11
--- /dev/null
+++ b/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/link/DisableLink.java
@@ -0,0 +1,38 @@
+/*
+ * 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.karaf.webconsole.osgi.scr.link;
+
+import org.apache.felix.scr.Component;
+import org.apache.wicket.Session;
+import org.apache.wicket.model.IModel;
+import org.osgi.framework.Bundle;
+
+public class DisableLink extends ScrLink {
+
+    private static final long serialVersionUID = 1L;
+
+    public DisableLink(String id, IModel<Bundle> model, String componentName) {
+        super(id, model, componentName);
+    }
+
+    @Override
+    public void onClick(Component component) {
+        component.disable();
+        Session.get().info("Component disabled successfully");
+    }
+
+}
diff --git a/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/link/EnableLink.java b/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/link/EnableLink.java
new file mode 100644
index 0000000..03291a2
--- /dev/null
+++ b/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/link/EnableLink.java
@@ -0,0 +1,38 @@
+/*
+ * 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.karaf.webconsole.osgi.scr.link;
+
+import org.apache.felix.scr.Component;
+import org.apache.wicket.Session;
+import org.apache.wicket.model.IModel;
+import org.osgi.framework.Bundle;
+
+public class EnableLink extends ScrLink {
+
+    private static final long serialVersionUID = 1L;
+
+    public EnableLink(String id, IModel<Bundle> model, String componentName) {
+        super(id, model, componentName);
+    }
+
+    @Override
+    public void onClick(Component component) {
+        component.enable();
+        Session.get().info("Component enabled successfully");
+    }
+
+}
diff --git a/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/link/ScrLink.java b/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/link/ScrLink.java
new file mode 100644
index 0000000..3f8e02b
--- /dev/null
+++ b/osgi/scr/src/main/java/org/apache/karaf/webconsole/osgi/scr/link/ScrLink.java
@@ -0,0 +1,64 @@
+/*
+ * 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.karaf.webconsole.osgi.scr.link;
+
+import org.apache.felix.scr.Component;
+import org.apache.felix.scr.ScrService;
+import org.apache.karaf.webconsole.osgi.scr.ScrComponent;
+import org.apache.wicket.Session;
+import org.apache.wicket.markup.html.link.Link;
+import org.apache.wicket.model.IModel;
+import org.osgi.framework.Bundle;
+
+public abstract class ScrLink extends Link<Bundle> {
+
+    private static final long serialVersionUID = 1L;
+
+    private String component;
+
+    public ScrLink(String id, IModel<Bundle> model, String componentName) {
+        super(id, model);
+
+        this.component = componentName;
+    }
+
+    @Override
+    public void onClick() {
+        ScrService scr = ScrComponent.getScrService();
+        if (scr == null) {
+            Session.get().warn("SCR service not found");
+            return;
+        }
+
+        Component[] components = scr.getComponents(getModelObject());
+        if (components == null) {
+            Session.get().warn("SCR components for bundle " + getModelObject().getSymbolicName() + " not found");
+            return;
+        }
+
+        for (Component component : components) {
+            if (this.component == component.getName()) {
+                onClick(component);
+                return;
+            }
+        }
+        Session.get().warn("SCR component named " + component + " not found in bundle " + getModelObject().getSymbolicName());
+    }
+
+    protected abstract void onClick(Component component);
+
+}
diff --git a/osgi/scr/src/main/resources/OSGI-INF/action.xml b/osgi/scr/src/main/resources/OSGI-INF/action.xml
new file mode 100644
index 0000000..ace31b1
--- /dev/null
+++ b/osgi/scr/src/main/resources/OSGI-INF/action.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!--
+   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.
+-->
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="scr-action-plugin">
+   <implementation class="org.apache.karaf.webconsole.osgi.scr.ScrActionProvider"/>
+
+   <service>
+      <provide interface="org.apache.karaf.webconsole.osgi.core.spi.IActionProvider"/>
+   </service>
+
+</scr:component>
diff --git a/osgi/scr/src/main/resources/org/apache/karaf/webconsole/osgi/scr/ScrActionPanel.html b/osgi/scr/src/main/resources/org/apache/karaf/webconsole/osgi/scr/ScrActionPanel.html
new file mode 100644
index 0000000..473ed67
--- /dev/null
+++ b/osgi/scr/src/main/resources/org/apache/karaf/webconsole/osgi/scr/ScrActionPanel.html
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   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.
+-->
+<wicket:panel xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
+
+    <ul class="unstyled">
+        <li>
+            <span><a>SCR components</a></span>
+        </li>
+        <li class="divider"></li>
+        <li wicket:id="components">
+            <a wicket:id="link">
+                <span wicket:id="label">[component]</span>
+            </a>
+        </li>
+    </ul>
+
+</wicket:panel>
\ No newline at end of file