Source Code Cross Referenced for LDAPNovell.java in  » Groupware » hipergate » com » knowgate » ldap » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Groupware » hipergate » com.knowgate.ldap 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        package com.knowgate.ldap;
0002:
0003:        /*
0004:         Copyright (C) 2004  Know Gate S.L. All rights reserved.
0005:         C/Oña, 107 1º2 28050 Madrid (Spain)
0006:
0007:         Redistribution and use in source and binary forms, with or without
0008:         modification, are permitted provided that the following conditions
0009:         are met:
0010:
0011:         1. Redistributions of source code must retain the above copyright
0012:         notice, this list of conditions and the following disclaimer.
0013:
0014:         2. The end-user documentation included with the redistribution,
0015:         if any, must include the following acknowledgment:
0016:         "This product includes software parts from hipergate
0017:         (http://www.hipergate.org/)."
0018:         Alternately, this acknowledgment may appear in the software itself,
0019:         if and wherever such third-party acknowledgments normally appear.
0020:
0021:         3. The name hipergate must not be used to endorse or promote products
0022:         derived from this software without prior written permission.
0023:         Products derived from this software may not be called hipergate,
0024:         nor may hipergate appear in their name, without prior written
0025:         permission.
0026:
0027:         This library is distributed in the hope that it will be useful,
0028:         but WITHOUT ANY WARRANTY; without even the implied warranty of
0029:         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0030:
0031:         You should have received a copy of hipergate License with this code;
0032:         if not, visit http://www.hipergate.org or mail to info@hipergate.org
0033:         */
0034:
0035:        import java.io.UnsupportedEncodingException;
0036:        import java.util.Vector;
0037:        import java.util.Properties;
0038:
0039:        import com.novell.ldap.LDAPAttribute;
0040:        import com.novell.ldap.LDAPAttributeSet;
0041:        import com.novell.ldap.LDAPEntry;
0042:        import com.novell.ldap.LDAPConnection;
0043:        import com.novell.ldap.LDAPSearchResults;
0044:        import com.novell.ldap.LDAPException;
0045:
0046:        import java.sql.Connection;
0047:        import java.sql.Statement;
0048:        import java.sql.PreparedStatement;
0049:        import java.sql.ResultSet;
0050:        import java.sql.SQLException;
0051:        import java.sql.ResultSetMetaData;
0052:
0053:        import com.knowgate.debug.DebugFile;
0054:        import com.knowgate.misc.Gadgets;
0055:        import com.knowgate.dataobjs.*;
0056:
0057:        /**
0058:         * Very Basic LDAP interface API implementation
0059:         * @author Sergio Montoro Ten
0060:         * @version 2.1
0061:         */
0062:
0063:        public class LDAPNovell extends LDAPModel {
0064:            private LDAPConnection oConn;
0065:            private String sBase;
0066:
0067:            public LDAPNovell() {
0068:                oConn = null;
0069:                sBase = null;
0070:            }
0071:
0072:            // ---------------------------------------------------------------------------
0073:
0074:            /**
0075:             * <p>Connect to LDAP Service</p>
0076:             * At this point, there is no authentication, and any operations are conducted as an anonymous client.
0077:             * @param sConnStr ldap://<i>host</i>:port/<i>distinguished_name</i><br><b>Example</b> "ldap://fobos.kg.int:389/dc=hipergate,dc=org"
0078:             * @throws com.knowgate.ldap.LDAPException
0079:             */
0080:            public void connect(String sConnStr)
0081:                    throws com.knowgate.ldap.LDAPException {
0082:
0083:                if (DebugFile.trace) {
0084:                    DebugFile.writeln("Begin LDAPNovell.connect(" + sConnStr
0085:                            + ")");
0086:                    DebugFile.incIdent();
0087:                }
0088:
0089:                if (sConnStr.startsWith("ldap://"))
0090:                    sConnStr = sConnStr.substring(7);
0091:
0092:                String sService = sConnStr.substring(0, sConnStr.indexOf('/'));
0093:                String[] aService = Gadgets.split2(sService, ':');
0094:
0095:                try {
0096:                    oConn = new LDAPConnection();
0097:                    if (aService.length < 2)
0098:                        oConn.connect(aService[0].trim(), 389);
0099:                    else
0100:                        oConn.connect(aService[0].trim(), Integer
0101:                                .parseInt(aService[1]));
0102:                    sBase = sConnStr.substring(sConnStr.indexOf('/') + 1);
0103:                } catch (com.novell.ldap.LDAPException xcpt) {
0104:                    if (DebugFile.trace)
0105:                        DebugFile.decIdent();
0106:                    sBase = null;
0107:                    if (DebugFile.trace) {
0108:                        DebugFile.writeln("LDAPNovell.connect() LDAPException "
0109:                                + xcpt.getMessage());
0110:                        DebugFile.decIdent();
0111:                    }
0112:                    throw new com.knowgate.ldap.LDAPException(
0113:                            xcpt.getMessage(), xcpt);
0114:                } catch (java.lang.NumberFormatException nfe) {
0115:                    if (DebugFile.trace) {
0116:                        DebugFile
0117:                                .writeln("LDAPNovell.connect() NumberFormatException Invalid port number");
0118:                        DebugFile.decIdent();
0119:                    }
0120:                    sBase = null;
0121:                    throw new com.knowgate.ldap.LDAPException(
0122:                            "Invalid port number", nfe);
0123:                }
0124:
0125:                if (DebugFile.trace) {
0126:                    DebugFile.decIdent();
0127:                    DebugFile.writeln("End LDAPNovell.connect()");
0128:                }
0129:            } // connect
0130:
0131:            // ---------------------------------------------------------------------------
0132:
0133:            /**
0134:             * <P>Connect to LDAP Server using a Properties object</P>
0135:             * @param oProps Properties for connecting to LDAP server.<BR>
0136:             * For example :<BR>
0137:             * ldapconnect=ldap://fobos.kg.int:389/dc=hipergate,dc=org<BR>
0138:             * ldapuser=cn=Manager,dc=hipergate,dc=org<BR>
0139:             * ldappassword=manager<BR>
0140:             * @throws com.knowgate.ldap.LDAPException
0141:             */
0142:            public void connectAndBind(Properties oProps)
0143:                    throws com.knowgate.ldap.LDAPException {
0144:
0145:                connect(oProps.getProperty("ldapconnect"));
0146:                bind(oProps.getProperty("ldapuser"), oProps
0147:                        .getProperty("ldappassword"));
0148:            }
0149:
0150:            // ---------------------------------------------------------------------------
0151:
0152:            /**
0153:             * <p>Synchronously disconnects from the LDAP server</p>
0154:             * The disconnect method abandons any outstanding requests, issues an unbind request to the server, and then closes the socket.
0155:             * @throws com.knowgate.ldap.LDAPException
0156:             */
0157:            public void disconnect() throws com.knowgate.ldap.LDAPException {
0158:
0159:                if (DebugFile.trace) {
0160:                    DebugFile.writeln("Begin LDAPNovell.disconnect()");
0161:                    DebugFile.incIdent();
0162:                }
0163:
0164:                try {
0165:                    if (oConn != null)
0166:                        oConn.disconnect();
0167:                    oConn = null;
0168:                } catch (com.novell.ldap.LDAPException xcpt) {
0169:                    if (DebugFile.trace)
0170:                        DebugFile.decIdent();
0171:                    throw new com.knowgate.ldap.LDAPException(
0172:                            xcpt.getMessage(), xcpt);
0173:                }
0174:
0175:                if (DebugFile.trace) {
0176:                    DebugFile.decIdent();
0177:                    DebugFile.writeln("End LDAPNovell.disconnect()");
0178:                }
0179:            } // disconnect
0180:
0181:            // ---------------------------------------------------------------------------
0182:
0183:            /**
0184:             * <p>Synchronously authenticates to the LDAP server using LDAP_V3.</p>
0185:             * If the object has been disconnected from an LDAP server, this method attempts to reconnect to the server. If the object has already authenticated, the old authentication is discarded.
0186:             * @param sUser If non-null and non-empty, specifies that the connection and all operations through it should be authenticated with dn as the distinguished name.
0187:             * @param sPass If non-null and non-empty, specifies that the connection and all operations through it should be authenticated with dn as the distinguished name and passwd as password.
0188:             * @throws LDAPException
0189:             * @throws IllegalStateException If not conencted to LDAP
0190:             */
0191:            public void bind(String sUser, String sPass)
0192:                    throws com.knowgate.ldap.LDAPException,
0193:                    IllegalStateException {
0194:
0195:                if (DebugFile.trace) {
0196:                    DebugFile.writeln("Begin LDAPNovell.bind(" + sUser
0197:                            + ",...)");
0198:                    DebugFile.incIdent();
0199:                }
0200:
0201:                if (null == oConn)
0202:                    throw new IllegalStateException("Not connected to LDAP");
0203:
0204:                try {
0205:                    oConn.bind(LDAPConnection.LDAP_V3, sUser, sPass
0206:                            .getBytes("UTF8"));
0207:                } catch (com.novell.ldap.LDAPException xcpt) {
0208:                    throw new com.knowgate.ldap.LDAPException(
0209:                            xcpt.getMessage(), xcpt);
0210:                } catch (java.io.UnsupportedEncodingException xcpt) {
0211:                    // never thrown
0212:                }
0213:
0214:                if (DebugFile.trace) {
0215:                    DebugFile.decIdent();
0216:                    DebugFile.writeln("End LDAPNovell.bind()");
0217:                }
0218:            } // bind
0219:
0220:            // ---------------------------------------------------------------------------
0221:
0222:            /**
0223:             * <p>Check whether or not an LDAP entry exists</p>
0224:             * The directory is searched from the connection string key.<br>
0225:             * For example if ldapconnect connection property is ldap://192.168.1.1:389/dc=hipergate,dc=org
0226:             * then only entries under "dc=hipergate,dc=org" will be searched
0227:             * @param sSearchString LDAP search string, for example "cn=user@mail.com,dc=publicContacts,dc=my_workarea,dc=my_domain"
0228:             * @throws com.knowgate.ldap.LDAPException
0229:             */
0230:            public boolean exists(String sSearchString)
0231:                    throws com.knowgate.ldap.LDAPException {
0232:
0233:                if (DebugFile.trace) {
0234:                    DebugFile.writeln("Begin LDAPNovell.exists("
0235:                            + sSearchString + ")");
0236:                    DebugFile.incIdent();
0237:                }
0238:
0239:                LDAPSearchResults searchResults = null;
0240:
0241:                try {
0242:                    searchResults = oConn.search(sBase,
0243:                            LDAPConnection.SCOPE_SUB, sSearchString,
0244:                            new String[] { "dn" }, true);
0245:                } catch (com.novell.ldap.LDAPException e) {
0246:                    throw new com.knowgate.ldap.LDAPException(e.getMessage(), e);
0247:                }
0248:
0249:                boolean bExists = searchResults.hasMore();
0250:
0251:                if (DebugFile.trace) {
0252:                    DebugFile.decIdent();
0253:                    DebugFile.writeln("End LDAPNovell.exists() : "
0254:                            + String.valueOf(bExists));
0255:                }
0256:
0257:                return bExists;
0258:            } // exists
0259:
0260:            // ---------------------------------------------------------------------------
0261:
0262:            private void addHive(String sDN, String sCN)
0263:                    throws com.knowgate.ldap.LDAPException {
0264:                LDAPAttributeSet attrs;
0265:
0266:                if (DebugFile.trace) {
0267:                    DebugFile.writeln("LDAPNovell.addHive(" + sDN + "," + sCN
0268:                            + ")");
0269:                }
0270:
0271:                try {
0272:                    attrs = new LDAPAttributeSet();
0273:                    attrs.add(new LDAPAttribute("objectClass", new String[] {
0274:                            "dcObject", "organizationalUnit" }));
0275:                    attrs.add(new LDAPAttribute("dc", sCN));
0276:                    attrs.add(new LDAPAttribute("ou", sCN));
0277:                    oConn.add(new LDAPEntry(sDN, attrs));
0278:                } catch (com.novell.ldap.LDAPException xcpt) {
0279:                    throw new com.knowgate.ldap.LDAPException(
0280:                            xcpt.getMessage(), xcpt);
0281:                }
0282:            }
0283:
0284:            // ---------------------------------------------------------------------------
0285:
0286:            private void addLeaf(String sDN, LDAPAttributeSet attrs)
0287:                    throws com.knowgate.ldap.LDAPException {
0288:
0289:                if (DebugFile.trace) {
0290:                    DebugFile.writeln("LDAPNovell.addLeaf(" + sDN + ", ...)");
0291:                }
0292:
0293:                try {
0294:                    attrs.add(new LDAPAttribute("objectClass", new String[] {
0295:                            "inetOrgPerson", "organizationalPerson" }));
0296:                    oConn.add(new LDAPEntry(sDN, attrs));
0297:                } catch (com.novell.ldap.LDAPException xcpt) {
0298:                    throw new com.knowgate.ldap.LDAPException(xcpt.getMessage()
0299:                            + " " + sDN, xcpt);
0300:                }
0301:            }
0302:
0303:            // ---------------------------------------------------------------------------
0304:
0305:            private LDAPAttributeSet mapJdbcToLdap(ResultSet oRSet,
0306:                    ResultSetMetaData oMDat) throws SQLException {
0307:
0308:                Object oFld;
0309:                String sFld;
0310:                String sCol;
0311:                LDAPAttributeSet oAttrs = new LDAPAttributeSet();
0312:
0313:                int iCols = oMDat.getColumnCount();
0314:
0315:                for (int c = 1; c <= iCols; c++) {
0316:                    oFld = oRSet.getObject(c);
0317:
0318:                    if (!oRSet.wasNull()) {
0319:                        sFld = oFld.toString();
0320:                        sCol = oMDat.getColumnName(c).toLowerCase();
0321:                        if (!sCol.startsWith("control_")) {
0322:                            oAttrs.add(new LDAPAttribute(sCol, sFld));
0323:                        }
0324:                    }
0325:                } // next
0326:
0327:                return oAttrs;
0328:            }
0329:
0330:            // ---------------------------------------------------------------------------
0331:
0332:            /**
0333:             * <p>Add an address from v_ldap_contacts view to an LDAP directory</p>
0334:             * Addresses may be either public or private depending on the value of field
0335:             * v_ldap_contacts.bo_private. If bo_private is zero then the address is public,
0336:             * if bo_private is not zero then the address is private.<br>
0337:             * Private addresses are only visible to the user that created them.<br>
0338:             * Public addresses are stored at cn=<i>user@mail.com</i>,dc=publicContacts,dc=<i>workarea_name</i>,dc=<i>domain_name</i>,dc=hipergate,dc=org<br>
0339:             * Private addresses are stored at cn=<i>user@mail.com</i>,dc=privateContacts,cn=<i>owner_guid</i>,dc=users,dc=<i>domain_name</i>,dc=hipergate,dc=org
0340:             * @param oJdbc JDBC Connection
0341:             * @param sAddrId GUID of address to be added
0342:             * @throws com.knowgate.ldap.LDAPException If address already exists at directory
0343:             * @throws SQLException If sAddrId is not found at v_ldap_contacts SQL view
0344:             * @throws IllegalStateException If not connected to LDAP
0345:             */
0346:            public void addAddress(Connection oJdbc, String sAddrId)
0347:                    throws com.knowgate.ldap.LDAPException,
0348:                    java.sql.SQLException, java.lang.IllegalStateException {
0349:
0350:                PreparedStatement oStmt;
0351:                ResultSet oRSet;
0352:                ResultSetMetaData oMDat;
0353:                LDAPAttributeSet oAttrs = null;
0354:                boolean bPrivate = true;
0355:                String sDN = null, sOwner = null, sTxEmail = null;
0356:
0357:                if (null == oConn)
0358:                    throw new IllegalStateException(
0359:                            "LDAPNovell.addAddress() Not connected to LDAP");
0360:
0361:                if (DebugFile.trace) {
0362:                    DebugFile
0363:                            .writeln("Begin LDAPNovell.addAddress([Connection], "
0364:                                    + sAddrId + ")");
0365:                    DebugFile.incIdent();
0366:                }
0367:
0368:                if (DebugFile.trace)
0369:                    DebugFile
0370:                            .writeln("Connection.prepareStatement(SELECT * FROM v_ldap_contacts WHERE \"uid\"="
0371:                                    + sAddrId + ")");
0372:
0373:                oStmt = oJdbc
0374:                        .prepareStatement(
0375:                                "SELECT * FROM v_ldap_contacts WHERE \"uid\"=?",
0376:                                ResultSet.TYPE_FORWARD_ONLY,
0377:                                ResultSet.CONCUR_READ_ONLY);
0378:                oStmt.setString(1, sAddrId);
0379:                oRSet = oStmt.executeQuery();
0380:                oMDat = oRSet.getMetaData();
0381:                boolean bFound = oRSet.next();
0382:
0383:                if (bFound) {
0384:                    sDN = "dc=" + oRSet.getString("control_workarea_name")
0385:                            + ",dc=" + oRSet.getString("control_domain_name")
0386:                            + "," + sBase;
0387:                    oAttrs = mapJdbcToLdap(oRSet, oMDat);
0388:                    bPrivate = (oRSet.getShort("control_priv") != (short) 0);
0389:                    sOwner = oRSet.getString("control_owner");
0390:                    sTxEmail = oRSet.getString("mail");
0391:                }
0392:
0393:                oRSet.close();
0394:                oStmt.close();
0395:
0396:                if (!bFound)
0397:                    throw new SQLException("Address " + sAddrId
0398:                            + " could not be found at v_ldap_contacts view",
0399:                            "01S06");
0400:
0401:                if (bPrivate) {
0402:                    // Contacto Privado
0403:                    addLeaf("cn=" + sTxEmail + "dc=privateContacts,cn="
0404:                            + sOwner + ",dc=users," + sDN, oAttrs);
0405:                } else {
0406:                    // Contacto Público (workarea)
0407:                    addLeaf("cn=" + sTxEmail + ",dc=publicContacts," + sDN,
0408:                            oAttrs);
0409:                }
0410:
0411:                if (DebugFile.trace) {
0412:                    DebugFile.decIdent();
0413:                    DebugFile.writeln("End LDAPNovell.addAddress()");
0414:                }
0415:            } // addAddress
0416:
0417:            // ---------------------------------------------------------------------------
0418:
0419:            /**
0420:             * <p>Add or replace an Address</p>
0421:             * This method is the same as addAddress() except that it does not raise an
0422:             * LDAPException if address already exists; in that case address is just replaced.
0423:             * @param oJdbc JDBC Connection
0424:             * @param sAddrId GUID of address to be added or replaced
0425:             * @throws com.knowgate.ldap.LDAPException
0426:             * @throws java.sql.SQLException
0427:             */
0428:            public void addOrReplaceAddress(Connection oJdbc, String sAddrId)
0429:                    throws com.knowgate.ldap.LDAPException,
0430:                    java.sql.SQLException {
0431:
0432:                PreparedStatement oStmt;
0433:                ResultSet oRSet;
0434:                ResultSetMetaData oMDat;
0435:                LDAPAttributeSet oAttrs = null;
0436:                boolean bPrivate = true;
0437:                String sDN = null, sOwner = null, sTxEmail = null;
0438:
0439:                if (null == oConn)
0440:                    throw new IllegalStateException(
0441:                            "LDAPNovell.addOrReplaceAddress() Not connected to LDAP");
0442:
0443:                if (DebugFile.trace) {
0444:                    DebugFile
0445:                            .writeln("Begin LDAPNovell.addOrReplaceAddress([Connection], "
0446:                                    + sAddrId + ")");
0447:                    DebugFile.incIdent();
0448:                }
0449:
0450:                if (DebugFile.trace)
0451:                    DebugFile
0452:                            .writeln("Connection.prepareStatement(SELECT * FROM v_ldap_contacts WHERE \"uid\"="
0453:                                    + sAddrId + ")");
0454:
0455:                oStmt = oJdbc
0456:                        .prepareStatement(
0457:                                "SELECT * FROM v_ldap_contacts WHERE \"uid\"=?",
0458:                                ResultSet.TYPE_FORWARD_ONLY,
0459:                                ResultSet.CONCUR_READ_ONLY);
0460:                oStmt.setString(1, sAddrId);
0461:                oRSet = oStmt.executeQuery();
0462:                oMDat = oRSet.getMetaData();
0463:                boolean bFound = oRSet.next();
0464:
0465:                if (bFound) {
0466:                    sDN = "dc=" + oRSet.getString("control_workarea_name")
0467:                            + ",dc=" + oRSet.getString("control_domain_name");
0468:                    oAttrs = mapJdbcToLdap(oRSet, oMDat);
0469:                    bPrivate = (oRSet.getShort("control_priv") != (short) 0);
0470:                    sOwner = oRSet.getString("control_owner");
0471:                    sTxEmail = oRSet.getString("mail");
0472:                }
0473:
0474:                oRSet.close();
0475:                oStmt.close();
0476:
0477:                if (!bFound)
0478:                    throw new SQLException("Address " + sAddrId
0479:                            + " could not be found at v_ldap_contacts view",
0480:                            "01S06");
0481:
0482:                if (bPrivate) {
0483:
0484:                    // Contacto Privado
0485:                    if (exists("cn=" + sTxEmail + "dc=privateContacts,cn="
0486:                            + sOwner + ",dc=users," + sDN + "," + sBase))
0487:                        deleteAddress(oJdbc, sAddrId);
0488:
0489:                    addLeaf("cn=" + sTxEmail + "dc=privateContacts,cn="
0490:                            + sOwner + ",dc=users," + sDN + "," + sBase, oAttrs);
0491:                } else {
0492:
0493:                    // Contacto Público (workarea)
0494:                    if (exists("cn=" + sTxEmail + ",dc=publicContacts," + sDN
0495:                            + "," + sBase))
0496:                        deleteAddress(oJdbc, sAddrId);
0497:
0498:                    addLeaf("cn=" + sTxEmail + ",dc=publicContacts," + sDN
0499:                            + "," + sBase, oAttrs);
0500:                }
0501:
0502:                if (DebugFile.trace) {
0503:                    DebugFile.decIdent();
0504:                    DebugFile.writeln("End LDAPNovell.addOrReplaceAddress()");
0505:                }
0506:            }
0507:
0508:            // ---------------------------------------------------------------------------
0509:
0510:            /**
0511:             * Delete an address from LDAP directory
0512:             * @param oJdbc JDBC Connection
0513:             * @param sAddrId GUID of address to be deleted
0514:             * @throws com.knowgate.ldap.LDAPException
0515:             * @throws SQLException If sAddrId is not found at v_ldap_contacts SQL view
0516:             * @throws IllegalStateException If not connected to LDAP
0517:             */
0518:            public void deleteAddress(Connection oJdbc, String sAddrId)
0519:                    throws com.knowgate.ldap.LDAPException,
0520:                    java.sql.SQLException, java.lang.IllegalStateException {
0521:
0522:                if (null == oConn)
0523:                    throw new IllegalStateException(
0524:                            "LDAPNovell.deleteAddress() Not connected to LDAP");
0525:
0526:                if (DebugFile.trace) {
0527:                    DebugFile
0528:                            .writeln("Begin LDAPNovell.deleteAddress([Connection], "
0529:                                    + sAddrId + ")");
0530:                    DebugFile.incIdent();
0531:                }
0532:
0533:                LDAPAttributeSet oAttrs;
0534:                PreparedStatement oStmt;
0535:                ResultSet oRSet;
0536:                boolean bPrivate = true;
0537:                String sDN = null, sOwner = null, sTxEmail = null;
0538:
0539:                if (DebugFile.trace)
0540:                    DebugFile
0541:                            .writeln("Connection.prepareStatement(SELECT * FROM v_ldap_contacts WHERE \"uid\"="
0542:                                    + sAddrId + ")");
0543:
0544:                oStmt = oJdbc
0545:                        .prepareStatement(
0546:                                "SELECT * FROM v_ldap_contacts WHERE \"uid\"=?",
0547:                                ResultSet.TYPE_FORWARD_ONLY,
0548:                                ResultSet.CONCUR_READ_ONLY);
0549:                oStmt.setString(1, sAddrId);
0550:                oRSet = oStmt.executeQuery();
0551:                boolean bFound = oRSet.next();
0552:
0553:                if (bFound) {
0554:                    sDN = "dc=" + oRSet.getString("control_workarea_name")
0555:                            + ",dc=" + oRSet.getString("control_domain_name")
0556:                            + "," + sBase;
0557:                    bPrivate = (oRSet.getShort("control_priv") != (short) 0);
0558:                    sOwner = oRSet.getString("control_owner");
0559:                    sTxEmail = oRSet.getString("mail");
0560:                }
0561:
0562:                oRSet.close();
0563:                oStmt.close();
0564:
0565:                if (!bFound)
0566:                    throw new SQLException("Address " + sAddrId
0567:                            + " could not be found at v_ldap_contacts view",
0568:                            "01S06");
0569:
0570:                if (bPrivate)
0571:                    sDN = "cn=" + sTxEmail + "dc=privateContacts,cn=" + sOwner
0572:                            + ",dc=users," + sDN;
0573:                else
0574:                    sDN = "cn=" + sTxEmail + ",dc=publicContacts," + sDN;
0575:
0576:                if (DebugFile.trace)
0577:                    DebugFile.writeln("LDAPConnection.delete(" + sDN + ")");
0578:
0579:                try {
0580:                    oConn.delete(sDN);
0581:                } catch (com.novell.ldap.LDAPException xcpt) {
0582:                    if (DebugFile.trace)
0583:                        DebugFile.decIdent();
0584:                    throw new com.knowgate.ldap.LDAPException(
0585:                            xcpt.getMessage(), xcpt);
0586:                }
0587:
0588:                if (DebugFile.trace) {
0589:                    DebugFile.decIdent();
0590:                    DebugFile.writeln("End LDAPNovell.deleteAddress()");
0591:                }
0592:            } // deleteAddress
0593:
0594:            // ---------------------------------------------------------------------------
0595:
0596:            /**
0597:             * <p>Add a User from v_ldap_users view to an LDAP directory</p>
0598:             * Users are added under cn=<i>user@mail.com</i>,dc=users,dc=<i>workarea_name</i>,dc=<i>domain_name</i>,dc=hipergate,dc=org
0599:             * @param oJdbc JDBC Connection
0600:             * @param sUserId GUID of user to be added
0601:             * @throws LDAPException
0602:             * @throws SQLException If sUserId is not found at v_ldap_users SQL view
0603:             * @throws IllegalStateException If not connected to LDAP
0604:             */
0605:            public void addUser(Connection oJdbc, String sUserId)
0606:                    throws com.knowgate.ldap.LDAPException,
0607:                    java.sql.SQLException, IllegalStateException {
0608:
0609:                PreparedStatement oStmt;
0610:                ResultSet oRSet;
0611:                ResultSetMetaData oMDat;
0612:                LDAPAttributeSet oAttrs = null;
0613:                String sDN = null, sTxEmail = null;
0614:
0615:                if (DebugFile.trace) {
0616:                    DebugFile.writeln("Begin LDAPNovell.addUser([Connection], "
0617:                            + sUserId + ")");
0618:                    DebugFile.incIdent();
0619:                }
0620:
0621:                if (DebugFile.trace)
0622:                    DebugFile
0623:                            .writeln("Connection.prepareStatement(SELECT * FROM v_ldap_users WHERE \"uid\"="
0624:                                    + sUserId + ")");
0625:
0626:                oStmt = oJdbc
0627:                        .prepareStatement(
0628:                                "SELECT * FROM v_ldap_users WHERE \"uid\"=?",
0629:                                ResultSet.TYPE_FORWARD_ONLY,
0630:                                ResultSet.CONCUR_READ_ONLY);
0631:                oStmt.setString(1, sUserId);
0632:                oRSet = oStmt.executeQuery();
0633:                oMDat = oRSet.getMetaData();
0634:                boolean bFound = oRSet.next();
0635:
0636:                if (bFound) {
0637:                    sDN = "dc=" + oRSet.getString("control_workarea_name")
0638:                            + ",dc=" + oRSet.getString("control_domain_name")
0639:                            + "," + sBase;
0640:
0641:                    oAttrs = mapJdbcToLdap(oRSet, oMDat);
0642:
0643:                    sTxEmail = oRSet.getString("mail");
0644:                }
0645:
0646:                oRSet.close();
0647:                oStmt.close();
0648:
0649:                if (!bFound)
0650:                    throw new SQLException("User " + sUserId
0651:                            + " could not be found at v_ldap_users view",
0652:                            "01S06");
0653:
0654:                addLeaf("cn=" + sTxEmail + ",dc=users," + sDN, oAttrs);
0655:
0656:                if (DebugFile.trace) {
0657:                    DebugFile.decIdent();
0658:                    DebugFile.writeln("End LDAPNovell.addUser()");
0659:                }
0660:            } // addUser
0661:
0662:            // ---------------------------------------------------------------------------
0663:
0664:            /**
0665:             * Add or replace a User from v_ldap_users SQL view to the LDAP directory
0666:             * @param oJdbc JDBC database connection
0667:             * @param sUserId GUID of user to be added or replaced
0668:             * @throws com.knowgate.ldap.LDAPException
0669:             * @throws java.sql.SQLException
0670:             */
0671:            public void addOrReplaceUser(Connection oJdbc, String sUserId)
0672:                    throws com.knowgate.ldap.LDAPException,
0673:                    java.sql.SQLException {
0674:
0675:                PreparedStatement oStmt;
0676:                ResultSet oRSet;
0677:                ResultSetMetaData oMDat;
0678:                LDAPAttributeSet oAttrs = null;
0679:                String sDN = null, sTxEmail = null;
0680:
0681:                if (DebugFile.trace) {
0682:                    DebugFile
0683:                            .writeln("Begin LDAPNovell.addOrReplaceUser([Connection], "
0684:                                    + sUserId + ")");
0685:                    DebugFile.incIdent();
0686:                }
0687:
0688:                if (DebugFile.trace)
0689:                    DebugFile
0690:                            .writeln("Connection.prepareStatement(SELECT * FROM v_ldap_users WHERE \"uid\"="
0691:                                    + sUserId + ")");
0692:
0693:                oStmt = oJdbc
0694:                        .prepareStatement(
0695:                                "SELECT * FROM v_ldap_users WHERE \"uid\"=?",
0696:                                ResultSet.TYPE_FORWARD_ONLY,
0697:                                ResultSet.CONCUR_READ_ONLY);
0698:                oStmt.setString(1, sUserId);
0699:                oRSet = oStmt.executeQuery();
0700:                oMDat = oRSet.getMetaData();
0701:                boolean bFound = oRSet.next();
0702:
0703:                if (bFound) {
0704:                    sDN = "dc=" + oRSet.getString("control_workarea_name")
0705:                            + ",dc=" + oRSet.getString("control_domain_name");
0706:
0707:                    oAttrs = mapJdbcToLdap(oRSet, oMDat);
0708:
0709:                    sTxEmail = oRSet.getString("mail");
0710:                }
0711:
0712:                oRSet.close();
0713:                oStmt.close();
0714:
0715:                if (!bFound)
0716:                    throw new SQLException("User " + sUserId
0717:                            + " could not be found at v_ldap_users view",
0718:                            "01S06");
0719:
0720:                if (exists("cn=" + sTxEmail + ",dc=users," + sDN))
0721:                    deleteUser(oJdbc, sUserId);
0722:
0723:                addLeaf("cn=" + sTxEmail + ",dc=users," + sDN + "," + sBase,
0724:                        oAttrs);
0725:
0726:                if (DebugFile.trace) {
0727:                    DebugFile.decIdent();
0728:                    DebugFile.writeln("End LDAPNovell.addOrReplaceUser()");
0729:                }
0730:            }
0731:
0732:            // ---------------------------------------------------------------------------
0733:
0734:            /**
0735:             * Delete a User from LDAP directory
0736:             * @param oJdbc JDBC Connection
0737:             * @param sUserId GUID of user to be added
0738:             * @throws com.knowgate.ldap.LDAPException
0739:             * @throws SQLException If sUserId is not found at v_ldap_users SQL view
0740:             */
0741:            public void deleteUser(Connection oJdbc, String sUserId)
0742:                    throws com.knowgate.ldap.LDAPException,
0743:                    java.sql.SQLException {
0744:
0745:                LDAPAttributeSet oAttrs;
0746:                PreparedStatement oStmt;
0747:                ResultSet oRSet;
0748:                String sTxEmail = null, sDN = null;
0749:
0750:                if (DebugFile.trace) {
0751:                    DebugFile
0752:                            .writeln("Begin LDAPNovell.deleteUser([Connection], "
0753:                                    + sUserId + ")");
0754:                    DebugFile.incIdent();
0755:                }
0756:
0757:                if (DebugFile.trace)
0758:                    DebugFile
0759:                            .writeln("Connection.prepareStatement(SELECT * FROM v_ldap_users WHERE \"uid\"="
0760:                                    + sUserId + ")");
0761:
0762:                oStmt = oJdbc
0763:                        .prepareStatement(
0764:                                "SELECT * FROM v_ldap_users WHERE \"uid\"=?",
0765:                                ResultSet.TYPE_FORWARD_ONLY,
0766:                                ResultSet.CONCUR_READ_ONLY);
0767:                oStmt.setString(1, sUserId);
0768:                oRSet = oStmt.executeQuery();
0769:                boolean bFound = oRSet.next();
0770:
0771:                if (bFound) {
0772:                    sTxEmail = oRSet.getString("mail");
0773:                    sDN = "cn=" + sTxEmail + ",dc=users," + "dc="
0774:                            + oRSet.getString("control_workarea_name") + ",dc="
0775:                            + oRSet.getString("control_domain_name") + ","
0776:                            + sBase;
0777:                }
0778:
0779:                oRSet.close();
0780:                oStmt.close();
0781:
0782:                if (!bFound)
0783:                    throw new SQLException("User " + sUserId
0784:                            + " could not be found at v_ldap_users view",
0785:                            "01S06");
0786:
0787:                if (DebugFile.trace)
0788:                    DebugFile.writeln("LDAPConnection.delete(" + sDN + ")");
0789:
0790:                try {
0791:                    oConn.delete(sDN);
0792:                } catch (com.novell.ldap.LDAPException xcpt) {
0793:                    if (DebugFile.trace)
0794:                        DebugFile.decIdent();
0795:                    throw new com.knowgate.ldap.LDAPException(
0796:                            xcpt.getMessage(), xcpt);
0797:                }
0798:
0799:                if (DebugFile.trace) {
0800:                    DebugFile.decIdent();
0801:                    DebugFile.writeln("End LDAPNovell.deleteUser()");
0802:                }
0803:            } // deleteUser
0804:
0805:            // ---------------------------------------------------------------------------
0806:
0807:            /**
0808:             * <P>Load all users and contact address from a Domain into an LDAP directory</P>
0809:             * @param oJdbc JDBC Connection
0810:             * @param iDomainId Numeric Identifier for Domain
0811:             * @throws com.knowgate.ldap.LDAPException
0812:             * @throws SQLException
0813:             */
0814:            public void loadDomain(Connection oJdbc, int iDomainId)
0815:                    throws com.knowgate.ldap.LDAPException,
0816:                    java.sql.SQLException {
0817:
0818:                LDAPAttributeSet oAttrs;
0819:                PreparedStatement oStmt;
0820:                ResultSet oRSet;
0821:                ResultSetMetaData oMDat;
0822:                String sDN, sDomainNm, sWorkAreaNm;
0823:                LDAPSearchResults searchResults = null;
0824:
0825:                if (DebugFile.trace) {
0826:                    DebugFile
0827:                            .writeln("Begin LDAPNovell.loadDomain([Connection]"
0828:                                    + String.valueOf(iDomainId) + ",...)");
0829:                    DebugFile.incIdent();
0830:                }
0831:
0832:                if (DebugFile.trace)
0833:                    DebugFile
0834:                            .writeln("Connection.prepareStatement(SELECT nm_domain FROM k_domains WHERE id_domain="
0835:                                    + String.valueOf(iDomainId) + ")");
0836:
0837:                oStmt = oJdbc
0838:                        .prepareStatement(
0839:                                "SELECT nm_domain FROM k_domains WHERE id_domain=?",
0840:                                ResultSet.TYPE_FORWARD_ONLY,
0841:                                ResultSet.CONCUR_READ_ONLY);
0842:                oStmt.setInt(1, iDomainId);
0843:                oRSet = oStmt.executeQuery();
0844:                oRSet.next();
0845:                sDomainNm = oRSet.getString(1);
0846:                oRSet.close();
0847:                oStmt.close();
0848:
0849:                // *************
0850:                // Create Domain
0851:
0852:                sDN = "dc=" + sDomainNm + "," + sBase;
0853:
0854:                try {
0855:                    searchResults = oConn.search(sBase,
0856:                            LDAPConnection.SCOPE_ONE, "(dc=" + sDomainNm + ")",
0857:                            new String[] { "dn" }, true);
0858:                } catch (com.novell.ldap.LDAPException e) {
0859:                    throw new com.knowgate.ldap.LDAPException(e.getMessage(), e);
0860:                }
0861:
0862:                if (!searchResults.hasMore())
0863:                    addHive(sDN, sDomainNm);
0864:
0865:                // ****************
0866:                // Create Workareas
0867:
0868:                if (DebugFile.trace)
0869:                    DebugFile
0870:                            .writeln("Connection.prepareStatement(SELECT nm_workarea FROM k_workareas WHERE id_domain="
0871:                                    + String.valueOf(iDomainId) + ")");
0872:
0873:                oStmt = oJdbc
0874:                        .prepareStatement(
0875:                                "SELECT nm_workarea FROM k_workareas WHERE id_domain=?",
0876:                                ResultSet.TYPE_FORWARD_ONLY,
0877:                                ResultSet.CONCUR_READ_ONLY);
0878:                oStmt.setInt(1, iDomainId);
0879:                oRSet = oStmt.executeQuery();
0880:
0881:                while (oRSet.next()) {
0882:                    sWorkAreaNm = oRSet.getString(1);
0883:
0884:                    sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + ","
0885:                            + sBase;
0886:
0887:                    try {
0888:                        searchResults = oConn.search("dc=" + sDomainNm + ","
0889:                                + sBase, LDAPConnection.SCOPE_ONE, "(dc="
0890:                                + sWorkAreaNm + ")", new String[] { "dn" },
0891:                                true);
0892:                    } catch (com.novell.ldap.LDAPException e) {
0893:                        throw new com.knowgate.ldap.LDAPException(e
0894:                                .getMessage(), e);
0895:                    }
0896:
0897:                    if (!searchResults.hasMore()) {
0898:                        // Primero crear la rama de la WorkArea
0899:                        addHive(sDN, sWorkAreaNm);
0900:
0901:                        // Despues se crean los subcontenedores necesarios
0902:                        addHive("dc=users," + sDN, "users");
0903:                        addHive("dc=publicContacts," + sDN, "publicContacts");
0904:                        addHive("dc=employees," + sDN, "employees");
0905:                    }
0906:                } // wend
0907:
0908:                oRSet.close();
0909:                oStmt.close();
0910:
0911:                // ***************
0912:                // Create Users
0913:
0914:                if (DebugFile.trace)
0915:                    DebugFile
0916:                            .writeln("Connection.prepareStatement(SELECT * FROM v_ldap_users WHERE control_domain_guid="
0917:                                    + String.valueOf(iDomainId) + ")");
0918:
0919:                oStmt = oJdbc
0920:                        .prepareStatement(
0921:                                "SELECT * FROM v_ldap_users WHERE control_domain_guid=?",
0922:                                ResultSet.TYPE_FORWARD_ONLY,
0923:                                ResultSet.CONCUR_READ_ONLY);
0924:                oStmt.setInt(1, iDomainId);
0925:                oRSet = oStmt.executeQuery();
0926:                oMDat = oRSet.getMetaData();
0927:
0928:                while (oRSet.next()) {
0929:                    sWorkAreaNm = oRSet.getString("control_workarea_name");
0930:                    sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + ","
0931:                            + sBase;
0932:
0933:                    oAttrs = mapJdbcToLdap(oRSet, oMDat);
0934:
0935:                    // Usuario
0936:                    addLeaf("cn=" + oRSet.getString("mail") + ",dc=users,"
0937:                            + sDN, oAttrs);
0938:                    // Añadir el contenedor de contactos privados
0939:                    addHive("dc=privateContacts,cn=" + oRSet.getString("mail")
0940:                            + ",dc=users," + sDN, "privateContacts");
0941:                } // wend
0942:
0943:                oRSet.close();
0944:                oStmt.close();
0945:
0946:                // ***************
0947:                // Create Contacts
0948:
0949:                if (DebugFile.trace)
0950:                    DebugFile
0951:                            .writeln("Connection.prepareStatement(SELECT * FROM v_ldap_contacts ld WHERE control_domain_guid="
0952:                                    + String.valueOf(iDomainId) + ")");
0953:
0954:                oStmt = oJdbc
0955:                        .prepareStatement(
0956:                                "SELECT * FROM v_ldap_contacts ld WHERE control_domain_guid=?",
0957:                                ResultSet.TYPE_FORWARD_ONLY,
0958:                                ResultSet.CONCUR_READ_ONLY);
0959:                oStmt.setInt(1, iDomainId);
0960:                oRSet = oStmt.executeQuery();
0961:                oMDat = oRSet.getMetaData();
0962:
0963:                while (oRSet.next()) {
0964:                    sWorkAreaNm = oRSet.getString("control_workarea_name");
0965:                    sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + ","
0966:                            + sBase;
0967:
0968:                    oAttrs = mapJdbcToLdap(oRSet, oMDat);
0969:
0970:                    if (oRSet.getShort("control_priv") != (short) 0) {
0971:                        // Contacto Privado
0972:                        addLeaf("cn=" + oRSet.getString("mail")
0973:                                + "dc=privateContacts,cn="
0974:                                + oRSet.getString("control_owner")
0975:                                + ",dc=users," + sDN, oAttrs);
0976:                    } else {
0977:                        // Contacto Público (workarea)
0978:                        addLeaf("cn=" + oRSet.getString("mail")
0979:                                + ",dc=publicContacts," + sDN, oAttrs);
0980:                    }
0981:                } // wend
0982:
0983:                if (DebugFile.trace) {
0984:                    DebugFile.decIdent();
0985:                    DebugFile.writeln("End LDAPNovell.loadDomain()");
0986:                }
0987:            } // loadDomain
0988:
0989:            // ---------------------------------------------------------------------------
0990:
0991:            /**
0992:             * <P>Load all users and contact address from a WorkArea into an LDAP directory</P>
0993:             * @param oJdbc JDBC Connection
0994:             * @param sDomainNm Name for Domain containing the WorkArea
0995:             * @param sWorkAreaNm WorkArea Name
0996:             * @throws com.knowgate.ldap.LDAPException
0997:             * @throws SQLException
0998:             */
0999:            public void loadWorkArea(Connection oJdbc, String sDomainNm,
1000:                    String sWorkAreaNm) throws com.knowgate.ldap.LDAPException,
1001:                    java.sql.SQLException {
1002:
1003:                LDAPAttributeSet oAttrs;
1004:                String sDN;
1005:                LDAPSearchResults searchResults = null;
1006:
1007:                if (DebugFile.trace) {
1008:                    DebugFile
1009:                            .writeln("Begin LDAPNovell.loadWorkArea([Connection]"
1010:                                    + sDomainNm + "," + sWorkAreaNm + ",...)");
1011:                    DebugFile.incIdent();
1012:                }
1013:
1014:                // **********************************
1015:                // Create Domain if it does not exist
1016:
1017:                sDN = "dc=" + sDomainNm + "," + sBase;
1018:
1019:                try {
1020:                    searchResults = oConn.search(sBase,
1021:                            LDAPConnection.SCOPE_ONE, "(dc=" + sDomainNm + ")",
1022:                            new String[] { "dn" }, true);
1023:                } catch (com.novell.ldap.LDAPException e) {
1024:                    throw new com.knowgate.ldap.LDAPException(e.getMessage(), e);
1025:                }
1026:
1027:                if (!searchResults.hasMore())
1028:                    addHive(sDN, sDomainNm);
1029:
1030:                // ***************
1031:                // Create WorkArea
1032:
1033:                sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + "," + sBase;
1034:
1035:                try {
1036:                    searchResults = oConn.search("dc=" + sDomainNm + ","
1037:                            + sBase, LDAPConnection.SCOPE_ONE, "(dc="
1038:                            + sWorkAreaNm + ")", new String[] { "dn" }, true);
1039:                } catch (com.novell.ldap.LDAPException e) {
1040:                    throw new com.knowgate.ldap.LDAPException(e.getMessage(), e);
1041:                }
1042:
1043:                if (!searchResults.hasMore()) {
1044:                    // Primero crear la rama de la WorkArea
1045:                    addHive(sDN, sWorkAreaNm);
1046:
1047:                    // Despues se crean los subcontenedores necesarios
1048:                    addHive("dc=users," + sDN, "users");
1049:                    addHive("dc=publicContacts," + sDN, "publicContacts");
1050:                    addHive("dc=employees," + sDN, "employees");
1051:                } // fi
1052:
1053:                // ***************
1054:                // Create Users
1055:
1056:                PreparedStatement oStmt = oJdbc
1057:                        .prepareStatement(
1058:                                "SELECT * FROM v_ldap_users WHERE control_domain_name=? AND control_workarea_name=?",
1059:                                ResultSet.TYPE_FORWARD_ONLY,
1060:                                ResultSet.CONCUR_READ_ONLY);
1061:                oStmt.setString(1, sDomainNm);
1062:                oStmt.setString(2, sWorkAreaNm);
1063:                ResultSet oRSet = oStmt.executeQuery();
1064:                ResultSetMetaData oMDat = oRSet.getMetaData();
1065:
1066:                while (oRSet.next()) {
1067:                    sWorkAreaNm = oRSet.getString("control_workarea_name");
1068:                    sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + ","
1069:                            + sBase;
1070:
1071:                    oAttrs = mapJdbcToLdap(oRSet, oMDat);
1072:
1073:                    // Usuario
1074:                    addLeaf("cn=" + oRSet.getString("mail") + ",dc=users,"
1075:                            + sDN, oAttrs);
1076:                    // Añadir el contenedor de contactos privados
1077:                    addHive("dc=privateContacts,cn=" + oRSet.getString("mail")
1078:                            + ",dc=users," + sDN, "privateContacts");
1079:                } // wend
1080:
1081:                oRSet.close();
1082:                oStmt.close();
1083:
1084:                // ***************
1085:                // Create Contacts
1086:
1087:                oStmt = oJdbc
1088:                        .prepareStatement(
1089:                                "SELECT * FROM v_ldap_contacts ld WHERE control_domain_name=? AND control_workarea_name=?",
1090:                                ResultSet.TYPE_FORWARD_ONLY,
1091:                                ResultSet.CONCUR_READ_ONLY);
1092:                oStmt.setString(1, sDomainNm);
1093:                oStmt.setString(2, sWorkAreaNm);
1094:                oRSet = oStmt.executeQuery();
1095:                oMDat = oRSet.getMetaData();
1096:
1097:                while (oRSet.next()) {
1098:                    sWorkAreaNm = oRSet.getString("control_workarea_name");
1099:                    sDN = "dc=" + sWorkAreaNm + ",dc=" + sDomainNm + ","
1100:                            + sBase;
1101:
1102:                    oAttrs = mapJdbcToLdap(oRSet, oMDat);
1103:
1104:                    if (oRSet.getShort("control_priv") != (short) 0) {
1105:                        // Contacto Privado
1106:                        addLeaf("cn=" + oRSet.getString("mail")
1107:                                + "dc=privateContacts,cn="
1108:                                + oRSet.getString("control_owner")
1109:                                + ",dc=users," + sDN, oAttrs);
1110:                    } else {
1111:                        // Contacto Público (workarea)
1112:                        addLeaf("cn=" + oRSet.getString("mail")
1113:                                + ",dc=publicContacts," + sDN, oAttrs);
1114:                    }
1115:                } // wend
1116:
1117:                if (DebugFile.trace) {
1118:                    DebugFile.decIdent();
1119:                    DebugFile.writeln("End LDAPNovell.loadWorkArea()");
1120:                }
1121:            } // loadWorkArea
1122:
1123:            // ---------------------------------------------------------------------------
1124:
1125:            /**
1126:             * Delete LDAP entry and all its childs
1127:             * @param oEntry LDAPEntry to be deleted
1128:             * @throws com.knowgate.ldap.LDAPException
1129:             * @throws IllegalStateException If not connected to LDAP
1130:             */
1131:            private void deleteEntry(LDAPEntry oEntry)
1132:                    throws com.knowgate.ldap.LDAPException,
1133:                    IllegalStateException {
1134:
1135:                if (null == oConn)
1136:                    throw new IllegalStateException(
1137:                            "LDAPNovell.deleteEntry() Not connected to LDAP");
1138:
1139:                String sDN = oEntry.getDN();
1140:                LDAPEntry nextEntry;
1141:
1142:                try {
1143:
1144:                    LDAPSearchResults searchResults = oConn.search(sDN,
1145:                            LDAPConnection.SCOPE_ONE, "(objectClass=*)",
1146:                            new String[] { "dn" }, true);
1147:
1148:                    while (searchResults.hasMore()) {
1149:                        try {
1150:                            nextEntry = searchResults.next();
1151:                        } catch (com.novell.ldap.LDAPException e) {
1152:                            continue;
1153:                        }
1154:
1155:                        deleteEntry(nextEntry);
1156:                    } // wend
1157:
1158:                    if (DebugFile.trace)
1159:                        DebugFile.writeln("LDAPConnection.delete(" + sDN + ")");
1160:
1161:                    oConn.delete(sDN);
1162:                } catch (com.novell.ldap.LDAPException xcpt) {
1163:                    throw new com.knowgate.ldap.LDAPException(
1164:                            xcpt.getMessage(), xcpt);
1165:                }
1166:            } // deleteEntry
1167:
1168:            // ---------------------------------------------------------------------------
1169:
1170:            /**
1171:             * <p>Delete a WorkArea from the LDAP directory</p>
1172:             * All entries under dc=<i>sDomainNm</i>,dc=hipergate,dc=org that match dc=<i>sWorkAreaNm</i> are deleted
1173:             * @param sDomainNm Domain Name
1174:             * @param sWorkAreaNm WorkArea Name
1175:             * @throws com.knowgate.ldap.LDAPException
1176:             * @throws IllegalStateException If not connected to LDAP
1177:             */
1178:            public void deleteWorkArea(String sDomainNm, String sWorkAreaNm)
1179:                    throws com.knowgate.ldap.LDAPException,
1180:                    IllegalStateException {
1181:
1182:                String sDN = "dc=" + sDomainNm + "," + sBase;
1183:                LDAPEntry oWrkAHive = null;
1184:
1185:                try {
1186:                    LDAPSearchResults searchResults = oConn.search(sDN,
1187:                            LDAPConnection.SCOPE_ONE, "(dc=" + sWorkAreaNm
1188:                                    + ")", new String[] { "dn" }, true);
1189:
1190:                    if (searchResults.hasMore()) {
1191:                        oWrkAHive = searchResults.next();
1192:                    }
1193:                } catch (com.novell.ldap.LDAPException e) {
1194:                    throw new com.knowgate.ldap.LDAPException(e.getMessage(), e);
1195:                }
1196:
1197:                if (oWrkAHive != null)
1198:                    deleteEntry(oWrkAHive);
1199:            } // deleteWorkArea
1200:
1201:            // ---------------------------------------------------------------------------
1202:
1203:            /**
1204:             * Drop an entire LDAP directory
1205:             * @throws com.knowgate.ldap.LDAPException
1206:             * @throws IllegalStateException If not connected to LDAP
1207:             */
1208:            public void dropAll() throws com.knowgate.ldap.LDAPException,
1209:                    IllegalStateException {
1210:
1211:                if (null == oConn)
1212:                    throw new IllegalStateException(
1213:                            "LDAPNovell.dropAll() Not connected to LDAP");
1214:
1215:                if (DebugFile.trace) {
1216:                    DebugFile.writeln("Begin LDAPNovell.dropAll()");
1217:                    DebugFile.incIdent();
1218:                }
1219:
1220:                LDAPSearchResults searchResults = null;
1221:                LDAPEntry nextEntry = null;
1222:
1223:                try {
1224:                    // Dropar todo el modelo de datos
1225:
1226:                    searchResults = oConn.search(sBase,
1227:                            LDAPConnection.SCOPE_ONE, "(objectClass=*)",
1228:                            new String[] { "dn" }, true);
1229:
1230:                    while (searchResults.hasMore()) {
1231:                        try {
1232:                            nextEntry = searchResults.next();
1233:                        } catch (LDAPException e) {
1234:                            continue;
1235:                        }
1236:
1237:                        if (!sBase.equals(nextEntry.getDN()))
1238:                            deleteEntry(nextEntry); // No borrar el elemento raíz!!!
1239:                    } // wend
1240:                } catch (com.novell.ldap.LDAPException xcpt) {
1241:                    throw new com.knowgate.ldap.LDAPException(
1242:                            xcpt.getMessage(), xcpt);
1243:                }
1244:
1245:                if (DebugFile.trace) {
1246:                    DebugFile.decIdent();
1247:                    DebugFile.writeln("End LDAPNovell.dropAll()");
1248:                }
1249:            } // dropAll
1250:
1251:            // ---------------------------------------------------------------------------
1252:
1253:            private static void printUsage() {
1254:                System.out.println("");
1255:                System.out.println("Usage:");
1256:                System.out.println("LDAPNovell path load all");
1257:                System.out
1258:                        .println("path: path to hipergate.cnf file ej. /opt/knowgate/hipergate.cnf");
1259:            }
1260:
1261:            public static void main(String[] argv)
1262:                    throws java.lang.ClassNotFoundException,
1263:                    java.io.IOException, java.sql.SQLException,
1264:                    com.knowgate.ldap.LDAPException {
1265:
1266:                if (argv.length < 3 || argv.length > 3)
1267:                    printUsage();
1268:                else {
1269:                    java.util.Properties oProps = new java.util.Properties();
1270:                    java.io.FileInputStream ioProps = new java.io.FileInputStream(
1271:                            argv[0]);
1272:                    oProps.load(ioProps);
1273:                    ioProps.close();
1274:
1275:                    LDAPNovell oLDP = new LDAPNovell();
1276:
1277:                    oLDP.connectAndBind(oProps);
1278:
1279:                    Class oDriver = Class.forName(oProps.getProperty("driver"));
1280:
1281:                    Connection oCon = java.sql.DriverManager.getConnection(
1282:                            oProps.getProperty("dburl"), oProps
1283:                                    .getProperty("dbuser"), oProps
1284:                                    .getProperty("dbpassword"));
1285:
1286:                    oLDP.dropAll();
1287:
1288:                    Statement oStm = oCon.createStatement();
1289:                    ResultSet oRst = oStm
1290:                            .executeQuery("SELECT id_domain FROM k_domains WHERE bo_active<>0");
1291:
1292:                    while (oRst.next()) {
1293:                        oLDP.loadDomain(oCon, oRst.getInt(1));
1294:                    }
1295:
1296:                    oRst.close();
1297:                    oStm.close();
1298:
1299:                    oCon.close();
1300:                    oLDP.disconnect();
1301:                }
1302:            }
1303:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.