001 /* 002 * Copyright 2006 Stephen J. McConnell. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 013 * implied. 014 * 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 package net.dpml.transit.management; 020 021 import java.lang.management.ManagementFactory; 022 import java.net.PasswordAuthentication; 023 import java.util.ArrayList; 024 import java.util.List; 025 import java.util.Hashtable; 026 027 import javax.management.MBeanServer; 028 import javax.management.ObjectName; 029 030 import javax.management.AttributeChangeNotification; 031 import javax.management.MBeanNotificationInfo; 032 import javax.management.NotificationBroadcasterSupport; 033 import javax.management.MBeanException; 034 035 import net.dpml.transit.Transit; 036 import net.dpml.transit.DefaultTransitModel; 037 import net.dpml.transit.model.TransitModel; 038 import net.dpml.transit.model.HostModel; 039 import net.dpml.transit.model.LayoutModel; 040 import net.dpml.transit.model.ProxyModel; 041 import net.dpml.transit.monitor.LoggingAdapter; 042 043 import net.dpml.util.Logger; 044 045 /** 046 * Transit MBean. 047 * 048 * @author <a href="http://www.dpml.net">Digital Product Meta Library</a> 049 * @version 0.0.0 050 */ 051 public class TransitController extends NotificationBroadcasterSupport implements TransitControllerMXBean 052 { 053 /** 054 * The Transit system version. 055 */ 056 public static final String VERSION = "0.0.0"; 057 058 private TransitModel m_model; 059 private Logger m_logger; 060 private List m_layouts = new ArrayList(); 061 private List m_hosts = new ArrayList(); 062 063 public TransitController( final Logger logger, final TransitModel model ) throws Exception 064 { 065 this( ManagementFactory.getPlatformMBeanServer(), logger, model ); 066 } 067 068 public TransitController( final MBeanServer server, final Logger logger, final TransitModel model ) throws Exception 069 { 070 m_model = model; 071 m_logger = logger; 072 073 Hashtable table = new Hashtable(); 074 table.put( "type", "Transit" ); 075 ObjectName name = 076 ObjectName.getInstance( "net.dpml", table ); 077 server.registerMBean( this, name ); 078 079 HostModel[] hosts = m_model.getCacheModel().getHostModels(); 080 for( int i=0; i < hosts.length; i++ ) 081 { 082 HostModel host = hosts[i]; 083 addHostModel( server, m_hosts, host ); 084 } 085 086 LayoutModel[] layouts = m_model.getCacheModel().getLayoutRegistryModel().getLayoutModels(); 087 for( int i=0; i < layouts.length; i++ ) 088 { 089 LayoutModel layout = layouts[i]; 090 addLayoutModel( server, m_layouts, layout ); 091 } 092 } 093 094 private void addLayoutModel( final MBeanServer server, final List list, final LayoutModel layout ) throws MBeanException 095 { 096 try 097 { 098 String id = layout.getID(); 099 Hashtable table = new Hashtable(); 100 table.put( "type", "TransitLayouts" ); 101 table.put( "name", id ); 102 ObjectName name = 103 ObjectName.getInstance( "net.dpml", table ); 104 LayoutController controller = new LayoutController( layout ); 105 server.registerMBean( controller, name ); 106 list.add( controller ); 107 } 108 catch( Exception e ) 109 { 110 throw new MBeanException( e ); 111 } 112 } 113 114 private void addHostModel( final MBeanServer server, final List list, final HostModel host ) throws MBeanException 115 { 116 try 117 { 118 String id = host.getID(); 119 Hashtable table = new Hashtable(); 120 table.put( "type", "TransitHosts" ); 121 table.put( "name", id ); 122 ObjectName name = 123 ObjectName.getInstance( "net.dpml", table ); 124 HostController controller = new HostController( host ); 125 list.add( controller ); 126 server.registerMBean( controller, name ); 127 } 128 catch( Exception e ) 129 { 130 throw new MBeanException( e ); 131 } 132 } 133 134 public String getProxyHost() throws MBeanException 135 { 136 try 137 { 138 return m_model.getProxyModel().getHost().toString(); 139 } 140 catch( Exception e ) 141 { 142 throw new MBeanException( e ); 143 } 144 } 145 146 public String getProxyUsername() throws MBeanException 147 { 148 try 149 { 150 PasswordAuthentication auth = m_model.getProxyModel().getAuthentication(); 151 if( null != auth ) 152 { 153 return auth.getUserName(); 154 } 155 else 156 { 157 return null; 158 } 159 } 160 catch( Exception e ) 161 { 162 throw new MBeanException( e ); 163 } 164 } 165 166 public String getProxyPassword() throws MBeanException 167 { 168 try 169 { 170 PasswordAuthentication auth = m_model.getProxyModel().getAuthentication(); 171 if( null != auth ) 172 { 173 return "*******"; 174 } 175 else 176 { 177 return null; 178 } 179 } 180 catch( Exception e ) 181 { 182 throw new MBeanException( e ); 183 } 184 } 185 public String getCacheDirectoryPath() throws MBeanException 186 { 187 try 188 { 189 return m_model.getCacheModel().getCacheDirectoryPath(); 190 } 191 catch( Exception e ) 192 { 193 throw new MBeanException( e ); 194 } 195 } 196 197 public String getCacheDirectory() throws MBeanException 198 { 199 try 200 { 201 return m_model.getCacheModel().getCacheDirectory().getCanonicalPath(); 202 } 203 catch( Exception e ) 204 { 205 throw new MBeanException( e ); 206 } 207 } 208 209 public String getCacheLayoutID() throws MBeanException 210 { 211 try 212 { 213 return m_model.getCacheModel().getLayoutModel().getID(); 214 } 215 catch( Exception e ) 216 { 217 throw new MBeanException( e ); 218 } 219 } 220 221 public String getCacheLayoutTitle() throws MBeanException 222 { 223 try 224 { 225 return m_model.getCacheModel().getLayoutModel().getTitle(); 226 } 227 catch( Exception e ) 228 { 229 throw new MBeanException( e ); 230 } 231 } 232 233 public String getHome() throws MBeanException 234 { 235 try 236 { 237 return Transit.DPML_HOME.getCanonicalPath(); 238 } 239 catch( Exception e ) 240 { 241 throw new MBeanException( e ); 242 } 243 } 244 245 public String getData() throws MBeanException 246 { 247 try 248 { 249 return Transit.DPML_DATA.getCanonicalPath(); 250 } 251 catch( Exception e ) 252 { 253 throw new MBeanException( e ); 254 } 255 } 256 257 public String getPrefs() throws MBeanException 258 { 259 try 260 { 261 return Transit.DPML_PREFS.getCanonicalPath(); 262 } 263 catch( Exception e ) 264 { 265 throw new MBeanException( e ); 266 } 267 } 268 269 public String getShare() throws MBeanException 270 { 271 try 272 { 273 return Transit.DPML_SYSTEM.getCanonicalPath(); 274 } 275 catch( Exception e ) 276 { 277 throw new MBeanException( e ); 278 } 279 } 280 281 public String getVersion() throws MBeanException 282 { 283 try 284 { 285 return Transit.VERSION; 286 } 287 catch( Exception e ) 288 { 289 throw new MBeanException( e ); 290 } 291 } 292 293 public LayoutControllerMXBean[] getLayouts() 294 { 295 return (LayoutControllerMXBean[]) m_layouts.toArray( new LayoutControllerMXBean[0] ); 296 } 297 298 public HostControllerMXBean[] getHosts() 299 { 300 return (HostControllerMXBean[]) m_hosts.toArray( new HostControllerMXBean[0] ); 301 } 302 }