5 Replies Latest reply on Sep 7, 2009 11:20 PM by ybxiang.china

    AOP does NOT work in User_Guide\gettingStarted\commandLineCl

    ybxiang.china

      AOP does NOT work in User_Guide\gettingStarted\commandLineClient.

      Hello, below is the test steps.

      1.I downloaded JBoss MC examples from
      http://anonsvn.jboss.org/repos/jbossas/projects/microcontainer/tags/2.2.0.M2/docs/examples/User_Guide
      and
      http://anonsvn.jboss.org/repos/jbossas/projects/microcontainer/tags/2.0.8.GA/docs/examples/User_Guide.
      2. In User_Guide\gettingStarted\humanResourcesService directory, i run "mvn install"

      3. In User_Guide\gettingStarted\auditAspect directory, i run "mvn install"

      4. In User_Guide\gettingStarted\commandLineClient\src\main\java\org\jboss\example\client directory, in run "mvn idea:idea" and "mvn install"

      5. I copy "aop-beans.xml" to \User_Guide\gettingStarted\commandLineClient\target\classes directory.
      and rename it to "jboss-beans.xml"

      6. Now, i start intelliJ by click "client.ipr" in User_Guide\gettingStarted\commandLineClient.

      7. I write a new client "GUIInput.java" in "User_Guide\gettingStarted\commandLineClient\src\main\java\org\jboss\example\client" directory. This class mocks the commandLineInput.

      8.Now, i can run this GUIInput Frame, deploy "jboss-beans.xml", i get the log:


      Client DEBUG [08-09-2009 10:47:29] KernelFactory - Starting JBoss Kernel construction...
      Client DEBUG [08-09-2009 10:47:29] KernelFactory - Completed JBoss Kernel construction. Duration: 453 milliseconds
      Client DEBUG [08-09-2009 10:47:33] SaxJBossXBParser - Created parser: org.apache.xerces.jaxp.SAXParserImpl@1277a30, isNamespaceAware: true, isValidating: true, isXIncludeAware: true
      Client DEBUG [08-09-2009 10:47:33] SaxJBossXBParser - http://xml.org/sax/features/validation set to: true
      Client DEBUG [08-09-2009 10:47:33] SaxJBossXBParser - http://xml.org/sax/features/namespaces set to: true
      Client DEBUG [08-09-2009 10:47:33] SaxJBossXBParser - http://apache.org/xml/features/validation/dynamic set to: true
      Client DEBUG [08-09-2009 10:47:33] SaxJBossXBParser - Using parser: org.apache.xerces.jaxp.SAXParserImpl@1277a30, isNamespaceAware: true, isValidating: true, isXIncludeAware: true
      Client DEBUG [08-09-2009 10:47:35] Aspect - Bound aspect AuditAspect; deployed:true
      ~~~~~This log shows the Aspect deployment is OK.

      But i can not find the log file in any place. I had searched auditLog, but found nothing.

      Who can tell me where is the log file?

      Thanks in advance!!!



        • 1. Re: AOP does NOT work in User_Guide\gettingStarted\commandLi
          ybxiang.china

          Here is the code of "org.jboss.example.client.GUIInput"

          
          public class GUIInput extends JFrame{
           private boolean useBus = false;
           //private boolean useBus = true;
           private URL url;
           //private UserInterface userInterface;
           private HRManager manager;
          
           private EmbeddedBootstrap bootstrap;
           private KernelController controller;
           private KernelBus bus;
          
           private final static String HRSERVICE = "HRService";
           private final static String EMPLOYEE = "org.jboss.example.service.Employee";
          
           int frame_width = 1024;
           int frame_height = 600;
          
           JLabel jl_UseBus = new JLabel("Use Bus");
           JComboBox jcb_UseBus = new JComboBox();
           JLabel jl_FirstName = new JLabel("First Name");
           JTextField jt_FirstName = new JTextField();
           JLabel jl_LastName = new JLabel("Last Name");
           JTextField jt_LastName = new JTextField();
           JLabel jl_Salary = new JLabel("Salary");
           JTextField jt_Salary = new JTextField();
           //
           JTextArea jt_Output = new JTextArea();
           //
           JButton jb_Deploy = new JButton("Deploy");
           JButton jb_Undeploy = new JButton("Uneploy");
           JButton jb_AddEmployee = new JButton("Add employee");
           JButton jb_ListEmployees = new JButton("List employees");
           JButton jb_RemoveEmployee = new JButton("Remove employee");
           JButton jb_GetASalary = new JButton("Get a salary");
           JButton jb_SetASalary = new JButton("Set a salary");
           JButton jb_ToggleHiringFreeze = new JButton("Toggle hiring freeze");
           JButton jb_PrintServiceStatus = new JButton("Print service status");
           JButton jb_Quit = new JButton("Quit");
           //
          
           public GUIInput(){
           try{
           {
           ClassLoader cl = Thread.currentThread().getContextClassLoader();
           url = cl.getResource("jboss-beans.xml");
          
           // Start JBoss Microcontainer
           bootstrap = new EmbeddedBootstrap();
           bootstrap.run();
          
           controller = bootstrap.getKernel().getController();
           bus = bootstrap.getKernel().getBus();
           }
          
           //
           this.addWindowListener(new WindowListener(){
           public void windowOpened(WindowEvent e){}
           public void windowClosing(WindowEvent e){System.exit(0);}
           public void windowClosed(WindowEvent e){System.exit(0);}
           public void windowIconified(WindowEvent e){}
           public void windowDeiconified(WindowEvent e){}
           public void windowActivated(WindowEvent e){}
           public void windowDeactivated(WindowEvent e){}
           });
           initGUI();
          
           }catch(Exception e){
           e.printStackTrace();
           }
          
           }
           private void initGUI(){
           JPanel jp_input = new JPanel();
           {
           jp_input.setLayout(new GridLayout(4,2));
           jp_input.setPreferredSize(new Dimension(frame_width-10,300));
           //
           jp_input.add(jl_UseBus);
           jp_input.add(jcb_UseBus);
           jp_input.add(jl_FirstName);
           jp_input.add(jt_FirstName);
           jp_input.add(jl_LastName);
           jp_input.add(jt_LastName);
           jp_input.add(jl_Salary);
           jp_input.add(jt_Salary);
           //
           jcb_UseBus.addItem(Boolean.TRUE);
           jcb_UseBus.addItem(Boolean.FALSE);
           jcb_UseBus.setSelectedItem(this.useBus);
           jcb_UseBus.addItemListener(new ItemListener(){
           public void itemStateChanged(ItemEvent e){
           if(jcb_UseBus.getSelectedItem()==Boolean.TRUE){
           GUIInput.this.useBus = true;
           }else{
           GUIInput.this.useBus = false;
           }
           System.out.println("GUIInput.this.useBus is set to "+GUIInput.this.useBus);
           }
           });
           }
           JPanel jp_output = new JPanel();
           {
           jp_output.setLayout(new GridLayout(1,2));
           jp_output.setPreferredSize(new Dimension(frame_width-10,150));
           jp_output.add(new JLabel("Output:"));
           jp_output.add(jt_Output);
           }
          
           JPanel jp_buttons = new JPanel();
           {
           jp_buttons.setPreferredSize(new Dimension(frame_width-10,100));
           jp_buttons.setLayout(new GridLayout(2,6));
           jp_buttons.add(jb_Deploy);
           jp_buttons.add(jb_Undeploy);
           jp_buttons.add(jb_AddEmployee);
           jp_buttons.add(jb_ListEmployees);
           jp_buttons.add(jb_RemoveEmployee);
           jp_buttons.add(jb_GetASalary);
           jp_buttons.add(jb_SetASalary);
           jp_buttons.add(jb_ToggleHiringFreeze);
           jp_buttons.add(jb_PrintServiceStatus);
           jp_buttons.add(jb_Quit);
           //
           jb_Deploy.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
           jb_Deploy__actionPerformed();
           }
           });
           jb_Undeploy.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
           jb_Undeploy__actionPerformed();
           }
           });
           jb_AddEmployee.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
           jb_AddEmployee__actionPerformed();
           }
           });
           jb_ListEmployees.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
           jb_ListEmployees__actionPerformed();
           }
           });
           jb_RemoveEmployee.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
           jb_RemoveEmployee__actionPerformed();
           }
           });
           jb_GetASalary.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
           jb_GetASalary__actionPerformed();
           }
           });
           jb_SetASalary.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
           jb_SetASalary__actionPerformed();
           }
           });
          
          
           jb_ToggleHiringFreeze.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
           jb_ToggleHiringFreeze__actionPerformed();
           }
           });
           jb_PrintServiceStatus.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
           jb_PrintServiceStatus__actionPerformed();
           }
           });
          
           jb_Quit.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
           System.exit(0);
           }
           });
           }
          
           this.setSize(frame_width,frame_width);
           this.getContentPane().add(jp_input, BorderLayout.NORTH);
           this.getContentPane().add(jp_output, BorderLayout.CENTER);
           this.getContentPane().add(jp_buttons, BorderLayout.SOUTH);
           }
          
           public static void main(String args[]){
           GUIInput gui = new GUIInput();
           gui.setTitle("JBoss Micro Container client");
           gui.setVisible(true);
           }
          
          
          
           public void jb_Deploy__actionPerformed(){
           bootstrap.deploy(url);
           if (!useBus && manager == null) {
           ControllerContext context = controller.getInstalledContext(HRSERVICE);
           if (context != null) {
           manager = (HRManager) context.getTarget();
           }
           }else{
          
           }
          
           jt_Output.setText("Deployed.");
           }
           public void jb_Undeploy__actionPerformed(){
           bootstrap.undeploy(url);
           jt_Output.setText("undeployed.");
           }
           public void jb_AddEmployee__actionPerformed (){
           try{
           Employee newEmployee = new Employee(this.jt_FirstName.getText(), this.jt_LastName.getText());
           newEmployee.setAddress(new Address(200030, "PuTuo", "Shanghai"));
           newEmployee.setDateOfBirth(new Date(new Date().getTime() - 2000000000000l));
          
           boolean result = false;
           if (useBus)
           result = (Boolean) invoke(HRSERVICE, "addEmployee", new Object[]{newEmployee}, new String[]{EMPLOYEE});
           else
           result = manager.addEmployee(newEmployee);
          
           jt_Output.setText("Add employee["+newEmployee+"] result:" + result);
          
           } catch (Exception e){
           e.printStackTrace();
           jt_Output.setText("Exception:\n"+e);
           }
          
           }
           public void jb_ListEmployees__actionPerformed(){
           Set<Employee> result;
           if (useBus)
           result = (Set<Employee>) invoke(HRSERVICE, "getEmployees", new Object[]{}, new String[]{});
           else
           result = manager.getEmployees();
          
           Iterator<Employee> it = result.iterator();
           jt_Output.setText("List All Employees:");
           while(it.hasNext()){
           Employee item = it.next();
           jt_Output.append("\n----:"+item);
           }
           jt_Output.append("\nList END");
          
           }
           public void jb_RemoveEmployee__actionPerformed(){
           try{
           Employee employee = new Employee(this.jt_FirstName.getText(), this.jt_LastName.getText());
           if (useBus)
           invoke(HRSERVICE, "removeEmployee", new Object[]{employee}, new String[]{EMPLOYEE});
           else
           manager.removeEmployee(employee);
           }catch(Exception e){
           e.printStackTrace();
           }
           }
           public void jb_GetASalary__actionPerformed(){
           try{
           Employee employee = new Employee(this.jt_FirstName.getText(), this.jt_LastName.getText());
           Integer result ;
           if (useBus)
           result = (Integer) invoke(HRSERVICE, "getSalary", new Object[]{employee}, new String[]{EMPLOYEE});
           else
           result = manager.getSalary(employee);
          
           jt_Output.setText(employee.toString()+"-salary is:"+result);
           }catch(Exception e){
           e.printStackTrace();
           }
           }
           public void jb_SetASalary__actionPerformed(){
           try{
           Employee employee = new Employee(this.jt_FirstName.getText(), this.jt_LastName.getText());
           Integer salary = Integer.parseInt(this.jt_Salary.getText());
          
           Employee actualEmployee;
           if (useBus) {
           actualEmployee = (Employee) invoke(HRSERVICE, "getEmployee", new Object[]{employee.getFirstName(), employee.getLastName()}, new String[]{"java.lang.String", "java.lang.String"});
           invoke(HRSERVICE, "setSalary", new Object[]{actualEmployee, salary}, new String[]{EMPLOYEE, "java.lang.Integer"});
           } else {
           actualEmployee = manager.getEmployee(employee.getFirstName(), employee.getLastName());
           manager.setSalary(actualEmployee, salary);
           }
          
           jt_Output.setText("set salary end.");
           }catch(Exception e){
           e.printStackTrace();
           }
           }
          
          
           public void jb_ToggleHiringFreeze__actionPerformed(){
           try{
           boolean hiringFreeze;
           if (useBus) {
           hiringFreeze = (Boolean) invoke(HRSERVICE, "isHiringFreeze", new Object[]{}, new String[]{});
           invoke(HRSERVICE, "setHiringFreeze", new Object[]{!hiringFreeze}, new String[]{"boolean"});
           } else {
           hiringFreeze = manager.isHiringFreeze();
           manager.setHiringFreeze(!hiringFreeze);
           }
           //return !hiringFreeze;
           jt_Output.setText("Now hiringFreeze="+hiringFreeze);
           }catch(Exception e){
           e.printStackTrace();
           }
           }
           public void jb_PrintServiceStatus__actionPerformed(){
           try{
           boolean hiringFreeze;
           int totalEmployees;
           SalaryStrategy salaryStrategy;
          
           if (useBus) {
           hiringFreeze = (Boolean) invoke(HRSERVICE, "isHiringFreeze", new Object[]{}, new String[]{});
           Set<Employee> employees = (Set<Employee>) invoke(HRSERVICE, "getEmployees", new Object[]{}, new String[]{});
           totalEmployees = employees.size();
           salaryStrategy = (SalaryStrategy) invoke(HRSERVICE, "getSalaryStrategy", new Object[]{}, new String[]{});
           } else {
           hiringFreeze = manager.isHiringFreeze();
           totalEmployees = manager.getEmployees().size();
           salaryStrategy = manager.getSalaryStrategy();
           }
          
           StringBuffer buffer = new StringBuffer();
           buffer.append("Total number of employees: " + totalEmployees);
           buffer.append("\nHiring Freeze: " + hiringFreeze);
           buffer.append("\nSalary Strategy: ");
           if (salaryStrategy == null) {
           buffer.append("None");
           } else {
           buffer.append(salaryStrategy.toString());
           }
          
          
           jt_Output.setText(buffer.toString());
           }catch(Exception e){
           jt_Output.setText("Exception:"+e);
           }
           }
          
           private Object invoke(String serviceName, String methodName, Object[] args, String[] types) {
           Object result = null;
           try {
           result = bus.invoke(serviceName, methodName, args, types);
           } catch (Throwable t) {
           t.printStackTrace();
           }
           return result;
           }
          
          }
          
          


          • 2. Re: AOP does NOT work in User_Guide\gettingStarted\commandLi
            ybxiang.china

            You can see that what GUIInput.java does is the same with ConsoleInput.java.

            The log tell me that i deployed "jboss-beans.xml" correctly.

            But, why AOP does NOT work?

            I write a new SalaryAspect,

            public Object audit(MethodInvocation inv) throws Throwable {
            return new Integer(987);
            }

            and config it, but it does not work too.


            • 3. Re: AOP does NOT work in User_Guide\gettingStarted\commandLi
              ybxiang.china

              Below is my jboss-beans.xml:


              <?xml version="1.0" encoding="UTF-8"?>
              
              <deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="urn:jboss:bean-deployer:2.0 bean-deployer_2_0.xsd"
               xmlns="urn:jboss:bean-deployer:2.0">
              
               <bean name="AspectManager" class="org.jboss.aop.AspectManager">
               <constructor factoryClass="org.jboss.aop.AspectManager" factoryMethod="instance"/>
               </bean>
              
               <aop:aspect xmlns:aop="urn:jboss:aop-beans:1.0"
               name="AuditAspect" class="org.jboss.example.aspect.AuditAspect"
               method="audit" pointcut="execution(public org.jboss.example.service.HRManager->new(..)) OR
               execution(public * org.jboss.example.service.HRManager->*(..))">
               </aop:aspect>
              
               <aop:aspect xmlns:aop="urn:jboss:aop-beans:1.0"
               name="SalaryAspect" class="org.jboss.example.aspect.SalaryAspect"
               method="audit" pointcut="execution(public org.jboss.example.service.HRManager->new(..)) OR
               execution(public * org.jboss.example.service.HRManager->getSalary(..))">
               </aop:aspect>
              
              
              
               <bean name="HRService" class="org.jboss.example.service.HRManager">
               <!-- <property name="hiringFreeze">true</property>
               <property name="salaryStrategy"><inject bean="AgeBasedSalary"/></property> -->
               </bean>
              
               <!-- <bean name="AgeBasedSalary" class="org.jboss.example.service.util.AgeBasedSalaryStrategy">
               <property name="minSalary">1000</property>
               <property name="maxSalary">80000</property>
               </bean>
              
               <bean name="LocationBasedSalary" class="org.jboss.example.service.util.LocationBasedSalaryStrategy">
               <property name="minSalary">2000</property>
               <property name="maxSalary">90000</property>
               </bean> -->
              
              </deployment>
              
              
              
              





              Below is the SalaryAspect written by myself:
              public class SalaryAspect {
              
              
               private String logDir;
               private BufferedWriter out;
              
               public SalaryAspect() {
               try{
               String filename = "Salary-Test.log";
               //File auditLog = new File(logDir + "/" + filename);
               File auditLog = new File(filename);
               auditLog.createNewFile();
               }catch(Exception e){
               e.printStackTrace();
               }
              
               logDir = System.getProperty("user.dir") + "/log";
               System.out.println("logDir="+logDir);
              
               File directory = new File(logDir);
               if (!directory.exists()) {
               directory.mkdir();
               }
               }
              
               public Object audit(ConstructorInvocation inv) throws Throwable {
               SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy-kkmmss");
               Calendar now = Calendar.getInstance();
               String filename = "Salary-" + formatter.format(now.getTime());
              
               //File auditLog = new File(logDir + "/" + filename);
               File auditLog = new File(filename);
               auditLog.createNewFile();
               out = new BufferedWriter(new FileWriter(auditLog));
               return inv.invokeNext();
               }
              
               public Object audit(MethodInvocation inv) throws Throwable {
               return new Integer(987);
               }
              }
              





              I am study JBOSS MC and hope to use it in my current project. So i hope JBOSS MC work perfectly.


              Any tips will be appreciated. Many thanks!

              • 4. Re: AOP does NOT work in User_Guide\gettingStarted\commandLi
                ybxiang.china

                I does NOT has linux installed, so i wrote the GUIInput.java to test examples in windows.

                Maybe there is something wrong with jar versions?
                Now, i will study JBoss AOP again, i hope i can solve this problem.

                It's a pity that i can NOT use AOP in excellent jboss MC. I had studied JBOss AS 5.0.GA, i find this server uses AOP correctly.

                So, i think this porblem can be solved definitly.

                • 5. Re: AOP does NOT work in User_Guide\gettingStarted\commandLi
                  ybxiang.china

                  More information,
                  when i modify jboss-beans.xml as below, GUIInput.java works as expected:

                  <?xml version="1.0" encoding="UTF-8"?>
                  
                  <deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="urn:jboss:bean-deployer:2.0 bean-deployer_2_0.xsd"
                   xmlns="urn:jboss:bean-deployer:2.0">
                  
                  
                   <bean name="HRService" class="org.jboss.example.service.HRManager">
                   <!--
                   <property name="hiringFreeze">true</property>
                   -->
                   <property name="salaryStrategy"><inject bean="AgeBasedSalary"/></property>
                  
                   </bean>
                  
                   <bean name="AgeBasedSalary" class="org.jboss.example.service.util.AgeBasedSalaryStrategy">
                   <property name="minSalary">1000</property>
                   <property name="maxSalary">80000</property>
                   </bean>
                  
                   <bean name="LocationBasedSalary" class="org.jboss.example.service.util.LocationBasedSalaryStrategy">
                   <property name="minSalary">2000</property>
                   <property name="maxSalary">90000</property>
                   </bean>
                  
                  </deployment>