View Javadoc

1       /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  package org.apache.james.management.impl;
20  
21  import org.apache.avalon.framework.service.ServiceException;
22  import org.apache.avalon.framework.service.ServiceManager;
23  import org.apache.avalon.framework.service.Serviceable;
24  import org.apache.james.services.SpoolManager;
25  import org.apache.james.management.ProcessorManagementMBean;
26  import org.apache.james.management.ProcessorManagementService;
27  import org.apache.james.management.mbean.MatcherManagement;
28  import org.apache.james.management.mbean.ProcessorDetail;
29  import org.apache.james.management.mbean.MailetManagement;
30  import org.apache.mailet.MailetConfig;
31  import org.apache.mailet.MatcherConfig;
32  
33  import javax.annotation.PostConstruct;
34  import javax.management.MBeanServer;
35  import javax.management.MBeanServerFactory;
36  import javax.management.MalformedObjectNameException;
37  import javax.management.ObjectName;
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * manage processors, mailets and matchers
44   */
45  public class ProcessorManagement implements Serviceable, ProcessorManagementService, ProcessorManagementMBean {
46  
47      private SpoolManager processorManager;
48  
49      public void service(ServiceManager serviceManager) throws ServiceException {
50          SpoolManager processorManager = (SpoolManager)serviceManager.lookup(SpoolManager.ROLE);
51          setProcessorManager(processorManager);
52      }
53  
54      @PostConstruct
55      public void init() {
56          registerMBeans();
57      }
58      
59      private void registerMBeans() {
60          ArrayList mBeanServers = MBeanServerFactory.findMBeanServer(null);
61          if (mBeanServers == null || mBeanServers.size() == 0) return; // no server to publish MBeans
62  
63          // take the first one
64          MBeanServer mBeanServer = (MBeanServer) mBeanServers.get(0);
65  
66          String baseObjectName = "Phoenix:application=james,topic=ProcessorAdministration,block=ProcessorManagement,";
67  
68          String[] processorNames = getProcessorNames();
69          for (int i = 0; i < processorNames.length; i++) {
70              String processorName = processorNames[i];
71              createProcessorMBean(baseObjectName, processorName, mBeanServer);
72              continue;
73          }
74      }
75  
76      private void createProcessorMBean(String baseObjectName, String processorName, MBeanServer mBeanServer) {
77          String processorMBeanName = baseObjectName + "processor=" + processorName;
78          ProcessorDetail processorMBean = new ProcessorDetail(processorManager, processorName);
79          registerMBean(mBeanServer, processorMBeanName, processorMBean);
80  
81          // add all mailets but the last, because that is a terminator (see LinearProcessor.closeProcessorLists())
82          List mailetConfigs = processorManager.getMailetConfigs(processorName);
83          for (int i = 0; i < mailetConfigs.size()-1; i++) {
84              MailetConfig mailetConfig = (MailetConfig) mailetConfigs.get(i);
85  
86              String mailetMBeanName = processorMBeanName + ",subtype=mailet,index=" + (i+1) + ",mailetname=" + mailetConfig.getMailetName();
87              MailetManagement mailetMBean = new MailetManagement(mailetConfig);
88              registerMBean(mBeanServer, mailetMBeanName, mailetMBean);
89          }
90  
91          // add all matchers but the last, because that is a terminator (see LinearProcessor.closeProcessorLists())
92          List matcherConfigs = processorManager.getMatcherConfigs(processorName);
93          for (int i = 0; i < matcherConfigs.size()-1; i++) {
94              MatcherConfig matcherConfig = (MatcherConfig) matcherConfigs.get(i);
95  
96              String matcherMBeanName = processorMBeanName + ",subtype=matcher,index=" + (i+1) + ",matchername=" + matcherConfig.getMatcherName();
97              MatcherManagement matcherMBean = new MatcherManagement(matcherConfig);
98              registerMBean(mBeanServer, matcherMBeanName, matcherMBean);
99          }
100 
101     }
102 
103     private void registerMBean(MBeanServer mBeanServer, String mBeanName, Object object) {
104         ObjectName objectName = null;
105         try {
106             objectName = new ObjectName(mBeanName);
107         } catch (MalformedObjectNameException e) {
108             return; // TODO handle error, log something
109         }
110         try {
111             mBeanServer.registerMBean(object, objectName);
112         } catch (javax.management.JMException e) {
113             e.printStackTrace(); // TODO change to logger
114         }
115     }
116 
117     public void setProcessorManager(SpoolManager processorManager) {
118         this.processorManager = processorManager;
119     }
120 
121     public String[] getProcessorNames() {
122         return processorManager.getProcessorNames();
123     }
124 
125     public String[] getMailetNames(String processorName) {
126         List mailetConfigs = processorManager.getMailetConfigs(processorName);
127         // always ommit the terminating mailet
128         String[] mailetNames = new String[mailetConfigs.size()-1];
129         int i = 0;
130         Iterator iterator = mailetConfigs.iterator();
131         while (iterator.hasNext()) {
132             MailetConfig mailetConfig = (MailetConfig) iterator.next();
133             if (!iterator.hasNext()) continue; // ommit the terminating mailet
134             String mailetName = mailetConfig.getMailetName();
135             mailetNames[i] = mailetName;
136             i++;
137         }
138         return mailetNames;
139     }
140 
141     public String[] getMatcherNames(String processorName) {
142         List matcherConfigs = processorManager.getMatcherConfigs(processorName);
143         // always ommit the terminating mailet
144         String[] matcherNames = new String[matcherConfigs.size()-1];
145         int i = 0;
146         Iterator iterator = matcherConfigs.iterator();
147         while (iterator.hasNext()) {
148             MatcherConfig matcherConfig = (MatcherConfig) iterator.next();
149             if (!iterator.hasNext()) continue; // ommit the terminating mailet
150             String matcherName = matcherConfig.getMatcherName();
151             matcherNames[i] = matcherName;
152             i++;
153         }
154         return matcherNames;
155     }
156     
157     public String[] getMatcherParameters(String processorName, int matcherIndex) {
158         List matcherConfigs = processorManager.getMatcherConfigs(processorName);
159         if (matcherConfigs == null || matcherConfigs.size() < matcherIndex) return null;
160         MatcherConfig matcherConfig = (MatcherConfig)matcherConfigs.get(matcherIndex);
161         return new String[] {matcherConfig.getCondition()};
162     }
163 
164     public String[] getMailetParameters(String processorName, int mailetIndex) {
165         List mailetConfigs = processorManager.getMailetConfigs(processorName);
166         if (mailetConfigs == null || mailetConfigs.size() < mailetIndex) return null;
167         MailetConfig mailetConfig = (MailetConfig) mailetConfigs.get(mailetIndex);
168         return MailetManagement.getMailetParameters(mailetConfig);
169     }
170 
171 }