Saturday, June 21, 2014

BEA-149500:An exception occurred while registering the MBean null

Last week, when I tried to deploy an ADF application to ADF managed server on WLS instance, I found below error in server logs. Of course, the application activated and worked without any other errors. But following error repeated every time when I tried deployment.

Error:

<Jun 17, 2014 9:32:01 AM CDT> <Error> <JMX> <BEA-149500> <An exception occurred while registering the MBean null.
java.lang.IllegalArgumentException: Registered more than one instance with the same objectName : com.bea:ServerRuntime=ADF_server,Name=HR,Type=ApplicationRuntime new:weblogic.j2ee.J2EEApplicationRuntimeMBeanImpl@1b017725 existing weblogic.j2ee.J2EEApplicationRuntimeMBeanImpl@1baabd61 at weblogic.management.jmx.ObjectNameManagerBase.registerObject(ObjectNameManagerBase.java:168)
 at weblogic.management.mbeanservers.internal.WLSObjectNameManager.lookupObjectName(WLSObjectNameManager.java:131)  at weblogic.management.jmx.modelmbean.WLSModelMBeanFactory.registerWLSModelMBean(WLSModelMBeanFactory.java:87) at weblogic.management.mbeanservers.internal.RuntimeMBeanAgent$1.registered(RuntimeMBeanAgent.java:105)         at weblogic.management.provider.core.RegistrationManagerBase.invokeRegistrationHandlers(RegistrationManagerBase.java:180)
     
Truncated. see log file for complete stacktrace>

Solution:

After checking the Weblogic configuration, I found that the data source name is same as the deployed application name causing the problem. To fix the error, I changed the application name and deployed it. No more errors.

To avoid this error in future, use unique names across Application,Data Source & JMS Queue etc..

Create tree menu structure and control page navigation in ADF

If we want to create tree menu structure, try following steps.

Steps:
1. Create TreeItem Object
2. Create Menu Bean class and add Menu nodes.
3. Add af:tree component to page and link to tree model.
4. Define navigation action

Tree Item Object
----------------------------------------------
import java.util.List;

public class TreeItem {
    private String text, action;
    private List<TreeItem> children;

    public TreeItem() {
        super();
    }

    public TreeItem(String text, String action) {
        super();
        this.text = text;
        this.action = action;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getAction() {
        return action;
    }

    public void setChildren(List<TreeItem> children) {
        this.children = children;
    }

    public List<TreeItem> getChildren() {
        return children;
    }
}


Menu Bean
--------------
import java.util.ArrayList;

import java.util.List;

import org.apache.myfaces.trinidad.model.ChildPropertyTreeModel;
import org.apache.myfaces.trinidad.model.TreeModel;

public class MenuBean {
    private Object instance = null;
    private transient TreeModel model = null;
    private ArrayList<TreeItem> root;

    public MenuBean() {
        root = new ArrayList<TreeItem>();
        TreeItem node1 = new TreeItem("Menu Header", "node1");
        root.add(node1);

        ArrayList<TreeItem> node1Children = new ArrayList<TreeItem>();
        TreeItem node1Child1 = new TreeItem("MenuChild1", "child1");
        node1Children.add(node1Child1);
        TreeItem node1Child2 = new TreeItem("MenuChild2", "child2");
        node1Children.add(node1Child2);
        TreeItem node1Child3 = new TreeItem("MenuChild2", "child3");
        node1Children.add(node1Child3);

        node1.setChildren(node1Children);

        setListInstance(root);
    }

    public TreeModel getModel() {
        if (model == null)
            model = new ChildPropertyTreeModel(instance, "children");
        return model;
    }

    public void setListInstance(List instance) {
        this.instance = instance;
        model = null;
    }
}


Page:
--------------
          <f:facet name="LeftFacet">
            <af:panelBox text="PanelBox1" id="pb1" showHeader="never"
                         showDisclosure="false">
              <af:tree value="#{menuBean.model}" var="node"
                       initiallyExpanded="true" rendered="#{sessionScope.isUserValid}">
                <f:facet name="nodeStamp">
                  <af:commandLink text="#{node.text}"
                                  actionListener="#{appBean.navNodeClicked}"
                                  id="ct1" partialSubmit="true"
                                  inlineStyle="font-weight:bold; font-size:9.0pt;"/>
                </f:facet>
              </af:tree>
            </af:panelBox>
          </f:facet>

Action:
-------------
    public void navNodeClicked(ActionEvent actionEvent) {
        // Add event code here...
        String linkId = ((RichCommandLink)actionEvent.getSource()).getText();

        AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
        Map<String, Object> pageFlowScope = adfFacesContext.getPageFlowScope();

        if (linkId.equals("MenuChild1")) {
            pageFlowScope.put("targetFlow", "child1");
        } else if (linkId.equals("MenuChild2")) {
            pageFlowScope.put("targetFlow", "child2");
        } else if (linkId.equals("MenuChild3")) {
            pageFlowScope.put("targetFlow", "child3");
        }
    }

Region in Page:
---------------
          <f:facet name="RightFacet">
            <af:group id="g1">
              <af:panelBox text="PanelBox1" id="pb2" showHeader="never"
                           showDisclosure="false">
                <af:region value="#{bindings.roottaskflow1.regionModel}"
                           binding="#{appBean.rightRegion}" id="r1"
                           partialTriggers="::pb1"
                           rendered="#{sessionScope.isUserValid}"/>
              </af:panelBox>
            </af:group>
          </f:facet>

Taskflow Parameter Definition in Page definition file:
--------------------------------------
  <executables>
    <variableIterator id="variables"/>
    <taskFlow id="roottaskflow1"
              taskFlowId="/WEB-INF/root-taskflow.xml#root-taskflow"
              activation="deferred"
              xmlns="http://xmlns.oracle.com/adf/controller/binding"
              Refresh="ifNeeded">
      <parameters>
        <parameter id="targetFlow" value="#{pageFlowScope.targetFlow}"/>
      </parameters>
    </taskFlow>
  </executables>

Taskflow Definition:
---------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2">
  <task-flow-definition id="root-taskflow">
    <default-activity id="__1">navigation</default-activity>
    <input-parameter-definition id="__10">
      <name id="__11">targetFlow</name>
      <value>#{pageFlowScope.targetFlow}</value>
      <class>java.lang.String</class>
    </input-parameter-definition>
    <router id="navigation">
     <case>
        <expression>#{pageFlowScope.targetFlow eq 'child1'}</expression>
        <outcome id="__12">child1</outcome>
      </case>
      <case>
        <expression>#{pageFlowScope.targetFlow eq 'child2'}</expression>
        <outcome id="__13">child2</outcome>
      </case>
      <case>
        <expression>#{pageFlowScope.targetFlow eq 'child3'}</expression>
        <outcome id="__17">child3</outcome>
      </case>    
      <default-outcome>child1</default-outcome>  
    </router>
    <task-flow-call id="child1-taskflow">
      <task-flow-reference>
        <document>/WEB-INF/child1-taskflow.xml</document>
        <id>child1-taskflow</id>
      </task-flow-reference>
    </task-flow-call>
    <task-flow-call id="child2-taskflow">
      <task-flow-reference>
        <document>/WEB-INF/child2-taskflow.xml</document>
        <id>child2-taskflow</id>
      </task-flow-reference>
    </task-flow-call>
    <task-flow-call id="child3-taskflow">
      <task-flow-reference>
        <document>/WEB-INF/child3-taskflow.xml</document>
        <id>child3-taskflow</id>
      </task-flow-reference>
    </task-flow-call>
    <control-flow-rule id="__2">
      <from-activity-id id="__3">navigation</from-activity-id>
      <control-flow-case id="__5">
        <from-outcome id="__6">child1</from-outcome>
        <to-activity-id id="__4">child1-taskflow</to-activity-id>
      </control-flow-case>
      <control-flow-case id="__7">
        <from-outcome id="__9">child2</from-outcome>
        <to-activity-id id="__8">child2-taskflow</to-activity-id>
      </control-flow-case>
      <control-flow-case id="__35">
        <from-outcome id="__33">child3</from-outcome>
        <to-activity-id id="__34">child3-taskflow</to-activity-id>
      </control-flow-case>
    </control-flow-rule>
    <use-page-fragments/>
  </task-flow-definition>
</adfc-config>

Expand/Collapse Tree Structure

Following post will help you to expand/collapse Tree Structure programmatically.

Create a tree structure surrounded by panel collection and having two buttons in panel collection tool bar to perform expand/collapse operations.

          <af:panelCollection id="pc1" styleClass="AFStretchWidth">
            <f:facet name="toolbar">
              <af:toolbar id="t1">
                <af:commandToolbarButton text="Expand All" id="cbb2"
                                         actionListener="#{empBean.expandEmpTreeTable}"
                                         disabled="#{bindings.EmployeeVO1.estimatedRowCount eq 0}"/>
                <af:spacer width="10" height="10" id="s21"/>
                <af:commandToolbarButton text="Collapse All" id="cbb3"
                                         actionListener="#{empBean.collapseEmpTreeTable}"
                                         disabled="#{bindings.EmployeeVO1.estimatedRowCount eq 0}"/>
              </af:toolbar>
            </f:facet>
            <af:treeTable value="#{bindings.EmployeeVO1.treeModel}" var="node"
                          selectionListener="#{bindings.EmployeeVO1.treeModel.makeCurrent}"
                          rowSelection="single" id="tt1"
                          styleClass="AFStretchWidth"
                          binding="#{empBean.empTree}" columnStretching="last"
                          disableColumnReordering="true" expandAllEnabled="true"
                          rowBandingInterval="1" summary="Display Employee Info"
                          autoHeightRows="24" partialTriggers="::cbb2 ::cbb3"
                          initiallyExpanded="true"
                          horizontalGridVisible="false">
              <f:facet name="nodeStamp">
                <af:column id="c1" width="250" headerText="Employee Name"
                           rowHeader="true" inlineStyle="text-align:start;">
                  <af:outputText id="ot1" value="#{node.EmpName}"
                                 rendered="#{node.EmpName != null ? true : false}"/>
                </af:column>
              </f:facet>
              <af:column id="c3" width="180" headerText="Email"
                         inlineStyle="#{node.NewCount gt '0' ? 'color:#00ff00;': 'color:#003D5B;'}">
                <af:outputText id="ot3" value="#{node.Email}"
                               rendered="#{node.Email != null ? true : false}"/>
              </af:column>
              <af:column id="c7" width="180" headerText="Selected"
                         inlineStyle="text-align:center;">
                <af:group id="g1">
                  <af:selectBooleanCheckbox id="sb2" autoSubmit="true"
                                            value="#{node.empCheck}"
                                            rendered="#{node.empCheck != null ? true : false}"
                                            label=" "/>
                </af:group>
              </af:column>
              <af:clientListener method="expandNode" type="selection"/>
            </af:treeTable>
          </af:panelCollection>

Create binding to tree table in bean class and add following methods to perform selected operation.

    private RichTreeTable empTree;

    public void setEmpTree(RichTreeTable empTree) {
        this.empTree = empTree;
    }

    public RichTreeTable getEmpTree() {
        return empTree;
    }

    private void expandTreeChildrenNode(RichTreeTable rt,
                                        JUCtrlHierNodeBinding node,
                                        List<Key> parentRowKey) {
        ArrayList children = node.getChildren();
        List<Key> rowKey;
        if (children != null) {
            for (int i = 0; i < children.size(); i++) {
                rowKey = new ArrayList<Key>();
                rowKey.addAll(parentRowKey);
                rowKey.add(((JUCtrlHierNodeBinding)children.get(i)).getRowKey());
                disclosedTreeRowKeySet.add(rowKey);
                if (((JUCtrlHierNodeBinding)(children.get(i))).getChildren() ==
                    null)
                    continue;
                expandTreeChildrenNode(rt,
                                       (JUCtrlHierNodeBinding)(node.getChildren().get(i)),
                                       rowKey);
            }
        }
    }

    public void collapseEmpTreeTable(ActionEvent actionEvent) {
        empTree.getDisclosedRowKeys().clear();
    }

    public void expandEmpTreeTable(ActionEvent actionEvent) {
        // Add event code here...
        if (this.empTree != null) {
            disclosedTreeRowKeySet = new RowKeySetImpl();

            CollectionModel model = (CollectionModel)empTree.getValue();

            JUCtrlHierBinding treeBinding =
                (JUCtrlHierBinding)model.getWrappedData();
            JUCtrlHierNodeBinding rootNode = treeBinding.getRootNodeBinding();
            disclosedTreeRowKeySet = empTree.getDisclosedRowKeys();
            if (disclosedTreeRowKeySet == null) {
                disclosedTreeRowKeySet = new RowKeySetImpl();
            }
            List<JUCtrlHierNodeBinding> firstLevelChildren =
                rootNode.getChildren();
            for (JUCtrlHierNodeBinding node : firstLevelChildren) {
                ArrayList list = new ArrayList();
                list.add(node.getRowKey());
                disclosedTreeRowKeySet.add(list);
                expandTreeChildrenNode(empTree, node, list);
            }
            empTree.setDisclosedRowKeys(disclosedTreeRowKeySet);
        }
    }

Expand/Collapse tree with single click:
Following Java script will help you to expand/collapse tree with single click

      <af:resource type="javascript">
        function expandNode(evt) {
            var tree = evt.getSource();
            var rwKeySet = evt.getAddedSet();
            var firstRowKey;
            for (rowKey in rwKeySet) {
                firstRowKey = rowKey;
                break;
            }
            if (tree.isPathExpanded(firstRowKey)) {
                tree.setDisclosedRowKey(firstRowKey, false);
            }
            else {
                tree.setDisclosedRowKey(firstRowKey, true);
            }
        }
      </af:resource>

Add clientlistener to treetable to trigger java script and set type as selection

       <af:clientListener method="expandNode" type="selection"/>