001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.server.ldap.handlers.extended;
021
022
023 import java.awt.Dimension;
024 import java.awt.Point;
025 import java.awt.Toolkit;
026 import java.util.Collections;
027 import java.util.HashSet;
028 import java.util.Set;
029
030 import javax.swing.JFrame;
031
032 import org.apache.directory.server.constants.ServerDNConstants;
033 import org.apache.directory.server.core.CoreSession;
034 import org.apache.directory.server.core.DirectoryService;
035 import org.apache.directory.server.core.authn.LdapPrincipal;
036 import org.apache.directory.server.core.interceptor.context.ListSuffixOperationContext;
037 import org.apache.directory.server.core.partition.Partition;
038 import org.apache.directory.server.core.partition.PartitionNexus;
039 import org.apache.directory.server.core.partition.impl.btree.BTreePartition;
040 import org.apache.directory.server.core.partition.impl.btree.gui.PartitionFrame;
041 import org.apache.directory.server.ldap.ExtendedOperationHandler;
042 import org.apache.directory.server.ldap.LdapServer;
043 import org.apache.directory.server.ldap.LdapSession;
044 import org.apache.directory.server.ldap.gui.SessionsFrame;
045 import org.apache.directory.shared.ldap.constants.AuthenticationLevel;
046 import org.apache.directory.shared.ldap.message.InternalExtendedRequest;
047 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
048 import org.apache.directory.shared.ldap.message.extended.LaunchDiagnosticUiRequest;
049 import org.apache.directory.shared.ldap.message.extended.LaunchDiagnosticUiResponse;
050 import org.apache.directory.shared.ldap.name.LdapDN;
051
052
053 /**
054 * @org.apache.xbean.XBean
055 *
056 */
057 public class LaunchDiagnosticUiHandler implements ExtendedOperationHandler
058 {
059 public static final Set<String> EXTENSION_OIDS;
060
061 static
062 {
063 Set<String> set = new HashSet<String>( 3 );
064 set.add( LaunchDiagnosticUiRequest.EXTENSION_OID );
065 set.add( LaunchDiagnosticUiResponse.EXTENSION_OID );
066 EXTENSION_OIDS = Collections.unmodifiableSet( set );
067 }
068
069 private LdapServer ldapServer;
070
071
072 public String getOid()
073 {
074 return LaunchDiagnosticUiRequest.EXTENSION_OID;
075 }
076
077
078 public void handleExtendedOperation( LdapSession requestor, InternalExtendedRequest req )
079 throws Exception
080 {
081 DirectoryService service = requestor.getCoreSession().getDirectoryService();
082
083 if ( ! requestor.getCoreSession().isAnAdministrator() )
084 {
085 requestor.getIoSession().write( new LaunchDiagnosticUiResponse( req.getMessageId(),
086 ResultCodeEnum.INSUFFICIENT_ACCESS_RIGHTS ) );
087 return;
088 }
089
090 requestor.getIoSession().write( new LaunchDiagnosticUiResponse( req.getMessageId() ) );
091
092 PartitionNexus nexus = service.getPartitionNexus();
093 LdapDN adminDn = new LdapDN( ServerDNConstants.ADMIN_SYSTEM_DN_NORMALIZED );
094 adminDn.normalize( service.getRegistries().getAttributeTypeRegistry().getNormalizerMapping() );
095 LdapPrincipal principal = new LdapPrincipal( adminDn, AuthenticationLevel.STRONG );
096 CoreSession session = service.getSession( principal );
097 Set<String> suffixes = nexus.listSuffixes( new ListSuffixOperationContext( session ) );
098 int launchedWindowCount = 0;
099
100 for ( String suffix:suffixes )
101 {
102 LdapDN dn = new LdapDN( suffix );
103 Partition partition = nexus.getPartition( dn );
104
105 if ( partition instanceof BTreePartition )
106 {
107 try
108 {
109 BTreePartition btPartition = ( BTreePartition ) partition;
110 // TODO : If a partition does not have an initial entry associated, we wil:
111 // get a NPE : this has to be fixed.
112 PartitionFrame frame = new PartitionFrame( btPartition, service.getRegistries() );
113 Point pos = getCenteredPosition( frame );
114 pos.y = launchedWindowCount * 20 + pos.y;
115 double multiplier = getAspectRatio() * 20.0;
116 pos.x = ( int ) ( launchedWindowCount * multiplier ) + pos.x;
117 frame.setLocation( pos );
118 frame.setVisible( true );
119 launchedWindowCount++;
120 }
121 catch ( Exception e )
122 {
123 // Continue
124 }
125 }
126 }
127
128 SessionsFrame sessions = new SessionsFrame( ldapServer );
129 sessions.setRequestor( requestor.getIoSession() );
130 sessions.setLdapProvider( ldapServer.getHandler() );
131 Point pos = getCenteredPosition( sessions );
132 pos.y = launchedWindowCount * 20 + pos.y;
133 double multiplier = getAspectRatio() * 20.0;
134 pos.x = ( int ) ( launchedWindowCount * multiplier ) + pos.x;
135 sessions.setLocation( pos );
136 sessions.setVisible( true );
137 }
138
139
140 public double getAspectRatio()
141 {
142 Toolkit tk = Toolkit.getDefaultToolkit();
143 Dimension screenSize = tk.getScreenSize();
144 return screenSize.getWidth() / screenSize.getHeight();
145 }
146
147
148 public Point getCenteredPosition( JFrame frame )
149 {
150 Point pt = new Point();
151 Toolkit tk = Toolkit.getDefaultToolkit();
152 Dimension screenSize = tk.getScreenSize();
153 pt.x = ( screenSize.width - frame.getWidth() ) / 2;
154 pt.y = ( screenSize.height - frame.getHeight() ) / 2;
155 return pt;
156 }
157
158
159 public Set<String> getExtensionOids()
160 {
161 return EXTENSION_OIDS;
162 }
163
164
165 public void setLdapServer( LdapServer ldapServer )
166 {
167 this.ldapServer = ldapServer;
168 }
169 }