// Sample code file: Populator.java
// Warning: This code has been marked up for HTML
/***************************************************************************
$name: Populator.java
$version: 1.0
$date_modified: 210104
$description:
$owner: Beans for Novell Services Team
Copyright (c) 1998 Novell, Inc. All Rights Reserved.
THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK.
PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
CUSTOMERS WITH RESPECT TO THIS CODE.
****************************************************************************/
//package ndsuam;
import java.util.*;
import java.lang.*;
import com.novell.beans.NDSUNIXUAM.*;
import com.novell.beans.NWIDirEntries.*;
import com.novell.beans.NWIDirQuery.*;
import com.novell.beans.idirutil.*;
import com.novell.beans.NWIDirUsrGrp.*;
//its the controller class which delegates work to different classes to carry on the business
//logic
public class Populator{
//instantite the vectors for holding objects like users, groups, ou and o
Vector m_o = new Vector(0);
Vector m_ou = new Vector(0);
Vector m_user = new Vector(0);
Vector m_group = new Vector(0);
Vector m_workStation = new Vector(0);
Authenticator m_auth = null;
//Constructor for this class, and this will also authenticate to directory,This will also get
//the collection of workstations present in directory.
public Populator(String fullName, String user, String passwd) throws Exception
{
m_auth = new Authenticator();
m_auth.authenticate(fullName, user, passwd);
refreshing();
}
//return the present context
public String getFullName()
{
return m_auth.getFullName();
}
//returns the collection of organizations at the preseent context
public Vector getOrganizations()
{
return m_o;
}
//returns the collection of organizationalUnits at the preseent context
public Vector getOrgUnits()
{
return m_ou;
}
//returns the collection of users at the preseent context
public Vector getUsers()
{
return m_user ;
}
//returns the collection of groups at the preseent context
public Vector getGroups()
{
return m_group ;
}
//returns the collection of workStations at the preseent context
public Vector getWorkStations()
{
return m_workStation;
}
//returns the collection of workStations at subtree level
public NDSUNIXWorkStations getAllWorkStations() throws Exception
{
return m_auth.getUAM().getWorkStations();
}
//returns instance of authenticator class
public Authenticator getAuthenticator()
{
return m_auth;
}
//change the context
public boolean changeLevel(String fullName) throws Exception
{
boolean ret = false;
if(m_auth.setFullName(fullName))
if(refreshing())
ret = true;
return ret;
}
//read from directory again
public boolean refresh(String fullName) throws Exception
{
boolean ret = false;
if(m_auth.setFullName(fullName))
if(refreshing())
ret = true;
return ret;
}
//clears the current connection
public void clear() throws Exception
{
m_auth.unauthenticate();
m_o.clear();
m_ou.clear();
m_user.clear();
m_group.clear();
m_workStation.clear();
}
//create a new user at the specified context
public boolean newUser(String fullName, String entryName) throws Exception
{
boolean ret = false;
try
{
m_auth.setFullName(fullName);
//get the instance of Entries bean
NWIDirEntries entries = m_auth.getEntries();
if (entries==null)
{
// System.out.println("Entry is null, exiting");
return ret;
//to be done error msg or exception
//or authenticator msgs to be shown.
}
int index = entryName.lastIndexOf("=");
String shortName = entryName.substring(index+1, entryName.length());
//create the entry object
NWEntry entry = new NWEntry(entryName, "inetOrgPerson");
//assign various mandatory attributes to the object
String [] objClass = {"inetOrgPerson","posixAccount"};
entry.setFieldValue("objectClass",objClass);
String [] surName = {""};
surName[0] = shortName;
entry.setFieldValue("cn", surName);
entry.setFieldValue("sn", surName);
entry.setFieldValue("uniqueID",surName);
String [] usrID = {"0"};
entry.setFieldValue("uidNumber",usrID);
String [] pgrpID = {"0"};
entry.setFieldValue("gidNumber",pgrpID);
String [] loginShell = {"/bin/sh"};
entry.setFieldValue("loginShell",loginShell);
String [] comments = {""};
entry.setFieldValue( "gecos",comments);
String [] homeDir = {""};
homeDir[0] = "/home/"+shortName;
entry.setFieldValue("homeDirectory",homeDir);
//add the object to the collection and in directory
entries.getEntries().addElement(entry);
//read from directory again to reflect changes in local variables
refreshing();
ret = true;
}
catch (Exception e)
{
throw e;
}
return ret;
}
//create a new group at the specified context
public boolean newGroup(String fullName, String entryName) throws Exception
{
boolean ret = false;
try
{
m_auth.setFullName(fullName);
//get the instance of Entries bean
NWIDirEntries entries = m_auth.getEntries();
if (entries==null)
{
// System.out.println("Entry is null, exiting");
return ret;
//to be done error msg or exception
//or authenticator msgs to be shown.
}
String[] cn= {""};
cn[0] = entryName.substring(entryName.lastIndexOf("=")+1, entryName.length());
//create the entry object
NWEntry entry = new NWEntry(entryName, "groupOfNames");
//assign various mandatory attributes to the object
String [] objClass = {"groupOfNames","posixGroup"};
entry.setFieldValue("objectClass",objClass);
String[] gidNo = {"0"};
entry.setFieldValue("gidNumber",gidNo);
entry.setFieldValue("cn",cn);
//add the object to the collection and in directory
entry = entries.getEntries().addElement(entry);
//read from directory again to reflect changes in local variables
refreshing();
ret = true;
}
catch (Exception e)
{
throw e;
}
return ret;
}
//create a new ou at the specified context
public boolean newOU(String fullName, String entryName) throws Exception
{
boolean ret = false;
try
{
m_auth.setFullName(fullName);
//get the instance of Entries bean
NWIDirEntries entries = m_auth.getEntries();
if (entries==null)
{
// System.out.println("Entry is null, exiting");
return ret;
//to be done error msg or exception
//or authenticator msgs to be shown.
}
//create the entry object
NWEntry entry = new NWEntry(entryName, "organizationalUnit");
//assign various mandatory attributes to the object
String [] objClass = {"organizationalUnit","top"};
entry.setFieldValue("objectClass",objClass);
//add the object to the collection and in directory
entries.getEntries().addElement(entry);
//read from directory again to reflect changes in local variables
refreshing();
ret = true;
}
catch (Exception e)
{
throw e;
}
return ret;
}
//delete the object
public boolean delObject(String objFullName) throws Exception
{
boolean ret = false;
try
{
int index = objFullName.lastIndexOf("/");
if (index == -1)
index = objFullName.lastIndexOf("\\");
String fullName = objFullName.substring(0,index);
if (getFullName().equalsIgnoreCase(fullName) ==false)
refresh(fullName);
//get the instance of Entries bean
NWIDirEntries entries = m_auth.getEntries();
NWEntries ents = entries.getEntries();
ents.refresh();
ents.removeElement(objFullName);
refreshing();
ret = true;
}
catch (Exception e)
{
throw e;
}
return ret;
}
//read from directory to populate local variables
boolean refreshing() throws Exception
{
boolean ret = false;
m_o.clear();
m_ou.clear();
m_user.clear();
m_group.clear();
m_workStation.clear();
try
{
//get the instance of the NWIDirQuery bean
NWIDirQuery dirq = m_auth.getQuery();
if (dirq==null)
{
// System.out.println("Query is null, exiting");
return ret;
//to be done error msg or exception
//or authenticator msgs to be shown.
}
//set the various flags according to which info willbe read from directory
dirq.setAutoReferrals(false);
String[] fields = {"cn","objectClass"};
dirq.setFields( fields );
dirq.setFilter("(|(objectClass=inetOrgPerson)(objectClass=posixAccount)(objectClass=group)(objectClass=posixGroup)(objectClass=uamPosixWorkstation)(objectClass=organizationalUnit)(objectClass=organization))");
//search the directory according to our search criteria specified above
NWQueryResult[] results = dirq.Search();
//iterate through results to populate different locale variables according to
//values obtained
for(int i=0;i<results.length;i++)
{
ResourceComponents rc = new ResourceComponents();
NWQueryResult result = results[i];
String resFullName = result.getFullName();
int reqStr = resFullName.lastIndexOf("/");
if (reqStr == -1)
reqStr = resFullName.lastIndexOf("\\");
String commaformat = resFullName.substring(reqStr+1);
resFullName = resFullName.substring(0,reqStr);
commaformat = rc.CommaToSlashFormat(commaformat);
resFullName = resFullName + "/" + commaformat;
//get the layout of this search result
String layout = result.getLayout();
//the result is a user object so add to users collection
if ((layout.indexOf("posixAccount")>=0)||(layout.indexOf("inetOrgPerson")>=0))
{
m_user.addElement(resFullName);
continue;
}
//the result is a ou object so add to ou collection
if (layout.indexOf("organizationalUnit")>=0)
{
m_ou.addElement(resFullName);
continue;
}
else
if (layout.indexOf("organization")>=0)
{
//the result is a organization object so add to organization collection
m_o.addElement(resFullName);
continue;
}
//the result is a Workstation object so add to Workstation collection
if (layout.indexOf("uamPosixWorkstation")>=0)
{
m_workStation.addElement(resFullName);
continue;
}
//the result is a group object so add to group collection
if (layout.indexOf("roup")>=0)
m_group.addElement(resFullName);
}
ret = true;
}
catch (Exception e)
{
throw e;
}
return ret;
}
}