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   */
20  package org.apache.directory.server.ldap.handlers.extended;
21  
22  
23  import java.awt.Dimension;
24  import java.awt.Point;
25  import java.awt.Toolkit;
26  import java.util.Collections;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.Set;
30  
31  import javax.swing.JFrame;
32  
33  import org.apache.directory.server.constants.ServerDNConstants;
34  import org.apache.directory.server.core.CoreSession;
35  import org.apache.directory.server.core.DirectoryService;
36  import org.apache.directory.server.core.authn.LdapPrincipal;
37  import org.apache.directory.server.core.interceptor.context.ListSuffixOperationContext;
38  import org.apache.directory.server.core.partition.Partition;
39  import org.apache.directory.server.core.partition.PartitionNexus;
40  import org.apache.directory.server.core.partition.impl.btree.BTreePartition;
41  import org.apache.directory.server.core.partition.impl.btree.gui.PartitionFrame;
42  import org.apache.directory.server.ldap.ExtendedOperationHandler;
43  import org.apache.directory.server.ldap.LdapService;
44  import org.apache.directory.server.ldap.LdapSession;
45  import org.apache.directory.server.ldap.gui.SessionsFrame;
46  import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
47  import org.apache.directory.shared.ldap.message.ExtendedRequest;
48  import org.apache.directory.shared.ldap.message.ResultCodeEnum;
49  import org.apache.directory.shared.ldap.message.extended.LaunchDiagnosticUiRequest;
50  import org.apache.directory.shared.ldap.message.extended.LaunchDiagnosticUiResponse;
51  import org.apache.directory.shared.ldap.name.LdapDN;
52  
53  
54  /**
55   * @org.apache.xbean.XBean
56   * 
57   */
58  public class LaunchDiagnosticUiHandler implements ExtendedOperationHandler
59  {
60      public static final Set<String> EXTENSION_OIDS;
61  
62      static
63      {
64          Set<String> set = new HashSet<String>( 3 );
65          set.add( LaunchDiagnosticUiRequest.EXTENSION_OID );
66          set.add( LaunchDiagnosticUiResponse.EXTENSION_OID );
67          EXTENSION_OIDS = Collections.unmodifiableSet( set );
68      }
69  
70      private LdapService ldapService;
71  
72  
73      public String getOid()
74      {
75          return LaunchDiagnosticUiRequest.EXTENSION_OID;
76      }
77  
78  
79      public void handleExtendedOperation( LdapSession requestor, ExtendedRequest req )
80          throws Exception
81      {
82          DirectoryService service = requestor.getCoreSession().getDirectoryService();
83          
84          if ( ! requestor.getCoreSession().isAnAdministrator() )
85          {
86              requestor.getIoSession().write( new LaunchDiagnosticUiResponse( req.getMessageId(),
87                  ResultCodeEnum.INSUFFICIENT_ACCESS_RIGHTS ) );
88              return;
89          }
90  
91          requestor.getIoSession().write( new LaunchDiagnosticUiResponse( req.getMessageId() ) );
92  
93          PartitionNexus nexus = service.getPartitionNexus();
94          LdapDN adminDn = new LdapDN( ServerDNConstants.ADMIN_SYSTEM_DN_NORMALIZED );
95          adminDn.normalize( service.getRegistries().getAttributeTypeRegistry().getNormalizerMapping() );
96          LdapPrincipal principal = new LdapPrincipal( adminDn, AuthenticationLevel.STRONG );
97          CoreSession session = service.getSession( principal );
98          Iterator<String> list = nexus.listSuffixes( new ListSuffixOperationContext( session ) );
99          int launchedWindowCount = 0;
100             
101         while ( list.hasNext() )
102         {
103             LdapDN dn = new LdapDN( list.next() );
104             Partition partition = nexus.getPartition( dn );
105             
106             if ( partition instanceof BTreePartition )
107             {
108                 BTreePartition btPartition = ( BTreePartition ) partition;
109                 PartitionFrame frame = new PartitionFrame( btPartition, service.getRegistries() );
110                 Point pos = getCenteredPosition( frame );
111                 pos.y = launchedWindowCount * 20 + pos.y;
112                 double multiplier = getAspectRatio() * 20.0;
113                 pos.x = ( int ) ( launchedWindowCount * multiplier ) + pos.x;
114                 frame.setLocation( pos );
115                 frame.setVisible( true );
116                 launchedWindowCount++;
117             }
118         }
119 
120         SessionsFrame sessions = new SessionsFrame( ldapService );
121         sessions.setRequestor( requestor.getIoSession() );
122         sessions.setLdapProvider( ldapService.getHandler() );
123         Point pos = getCenteredPosition( sessions );
124         pos.y = launchedWindowCount * 20 + pos.y;
125         double multiplier = getAspectRatio() * 20.0;
126         pos.x = ( int ) ( launchedWindowCount * multiplier ) + pos.x;
127         sessions.setLocation( pos );
128         sessions.setVisible( true );
129     }
130 
131 
132     public double getAspectRatio()
133     {
134         Toolkit tk = Toolkit.getDefaultToolkit();
135         Dimension screenSize = tk.getScreenSize();
136         return screenSize.getWidth() / screenSize.getHeight();
137     }
138 
139 
140     public Point getCenteredPosition( JFrame frame )
141     {
142         Point pt = new Point();
143         Toolkit tk = Toolkit.getDefaultToolkit();
144         Dimension screenSize = tk.getScreenSize();
145         pt.x = ( screenSize.width - frame.getWidth() ) / 2;
146         pt.y = ( screenSize.height - frame.getHeight() ) / 2;
147         return pt;
148     }
149 
150 
151     public Set<String> getExtensionOids()
152     {
153         return EXTENSION_OIDS;
154     }
155 
156 
157     public void setLdapServer( LdapService ldapService )
158     {
159         this.ldapService = ldapService;
160     }
161 }