Rounded Rectangle: JNDI Service Provider for Windows® Registries






Jeff Lawson
 

Rounded Rectangle: Overview

·	Introduction to Windows® Registries

·	Windows® Registries are Naming Services

·	 Accessing System Settings

·	 Accessing User Settings

·	 Security

·	 Java™ Objects

·	 Federation

·	 Event Notification

·	 Summary
 

Rounded Rectangle: Introduction to Windows® Registries

·	Windows registries hold system and user configuration data

·	Available on:
§	NT-class systems: Windows NT/2000/XP
§	Down-level systems:  Windows 95/98/Me

·	Accessible locally and remotely 

·	Hierarchical database that is transactional on NT-class systems

·	Supports several data types:
§	REG_DWORD — 32-bit integer
§	REG_STRING — single-line string
§	REG_MULTI_SZ — multi-line string
§	REG_BINARY — any data, e.g. Java™ objects
 

Rounded Rectangle: Windows® Registries are Naming Services

·	Bindings are called registry values and take the form:

<name>	<type>	<data>
e.g.	JAVA_HOME	REG_SZ	E:\Java\jdk1.3\

·	Naming contexts are called registry keys; they contain values and subkeys (subcontexts)

·	Windows registries are not directory services — no attributes

·	Registry paths use backslash as a name component separator so they have strong separation in composite names:

…/MyApp\1.0\PrimaryDataSource/…
		actually:	…/MyApp\\1.0\\PrimaryDataSource/…
 

Rounded Rectangle: Accessing System Settings

·	System settings are held in:

		HKEY_LOCAL_MACHINE

which has parts aliased as:

	HKEY_CLASSES_ROOT
		HKEY_PERFORMANCE_DATA	(Windows NT/2000/XP only)
HKEY_DYN_DATA				(Windows 95/98/Me only)

·	System settings cover:
§	Hardware:	disk drives, network adapters, display,
mouse, print devices, memory, etc.
§	Security:	users and groups
§	Software:	applications, user interface, COM
§	System:		services, drivers, control, hardware profiles

Rounded Rectangle:
 

Rounded Rectangle: Sample Code

import java.util.*;
import javax.naming.*;
// …
try
{
    // Set up to use Winreg factory
    Hashtable env = new Hashtable(5, 0.75f);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.cogentlogic.jndi.winreg.WinregInitContextFactory");
    env.put(Context.PROVIDER_URL, "winreg://localhost/HKEY_LOCAL_MACHINE");
    initctx = new InitialContext(env);
    System.out.println("Name in Namespace: " + initctx.getNameInNamespace());   // F.Y.I.

    String str;
    Object obj = initctx.lookup("SYSTEM\\CurrentControlSet\\Control\\
ComputerName\\ActiveComputerName\\ComputerName");
    if (obj instanceof String)	// known to be a string so no need to check!
    {
        str = (String)obj;
        System.out.println("ComputerName: " + str);
    }

    Context ctx = (Context)initctx.lookup("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters");
    System.out.println("HostName: " + (String)ctx.lookup("HostName"));		// cast not required
    System.out.println("Domain: " + ctx.lookup("Domain"));
    System.out.println("NameServer: " + ctx.lookup("NameServer"));

    ctx.close();
    initctx.close();
}										Name in Namespace: HKEY_LOCAL_MACHINE
catch (NamingException except)					ComputerName: BEAUTY
{										HostName: www
    except.printStackTrace(System.out);				Domain: cogentlogic.com
}										NameServer: 209.250.128.6 209.250.128.8

Rounded Rectangle: More Sample Code

// list
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.cogentlogic.jndi.winreg.WinregInitContextFactory");
env.put(Context.PROVIDER_URL, "winreg://localhost/HKEY_LOCAL_MACHINE\\SOFTWARE\\
Microsoft\\Windows NT\\CurrentVersion");
initctx = new InitialContext(env);
NamingEnumeration enum = initctx.list("Time Zones");
NameClassPair ncp;
while (enum.hasMore())
{
    ncp = (NameClassPair)enum.next();
    System.out.println(ncp);
}
initctx.close();


Afghanistan Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Alaskan Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Arabian Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Atlantic Standard Time: com.cogentlogic.jndi.winreg.WinregContext
AUS Central Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Azores Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Bangkok Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Canada Central Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Caucasus Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Cen. Australia Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Central Asia Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Central Europe Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Central European Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Central Pacific Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Central Standard Time: com.cogentlogic.jndi.winreg.WinregContext
China Standard Time: com.cogentlogic.jndi.winreg.WinregContext
Dateline Standard Time: com.cogentlogic.jndi.winreg.WinregContext
E. Africa Standard Time: com.cogentlogic.jndi.winreg.WinregContext

Rounded Rectangle: Yet More Sample Code

// listBindings
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.cogentlogic.jndi.winreg.WinregInitContextFactory");
env.put(Context.PROVIDER_URL, "winreg://localhost/HKEY_LOCAL_MACHINE\\SOFTWARE\\
ODBC\\odbc.ini\\ODBC Data Sources");
initctx = new InitialContext(env);
enum = initctx.listBindings("");
Binding bd;
while (enum.hasMore())
{
    bd = (Binding)enum.next();
    System.out.println(bd.getName() + ": " + bd.getObject());
}
initctx.close();



AdvWorks: Microsoft Access Driver (*.mdb)
CertSrv: Microsoft Access Driver (*.mdb)
CogentLogicEmail: SQL Server
WBEM Source: WBEM ODBC Driver
Biblio: Microsoft Access Driver (*.mdb)
NWind: Microsoft Access Driver (*.mdb)
LocalServer: SQL Server
MQIS: SQL Server
configure: SQL Server

Rounded Rectangle: Remote Access

// Use anonymous context
System.out.println("TimeZone: " + new InitialContext().lookup("winreg://TESLA/HKEY_LOCAL_MACHINE\\
    SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation\\StandardName"));

// Set up to use Winreg factory
Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.cogentlogic.jndi.winreg.WinregInitContextFactory");
env.put(Context.PROVIDER_URL, "winreg://TESLA/HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\
Windows NT\\CurrentVersion");
initctx = new InitialContext(env);
System.out.println("CSDVersion: " + initctx.lookup("CSDVersion"));
System.out.println("CurrentVersion: " + initctx.lookup("CurrentVersion"));
System.out.println("RegisteredOrganization: " + initctx.lookup("RegisteredOrganization"));
System.out.println("RegisteredOwner: " + initctx.lookup("RegisteredOwner"));
System.out.println("SystemRoot: " + initctx.lookup("SystemRoot"));
initctx.close();





TimeZone: Eastern Standard Time
CSDVersion: Service Pack 6
CurrentVersion: 4.0
RegisteredOrganization: Cogent Logic Corporation
RegisteredOwner: Jeff Lawson
SystemRoot: C:\WINNT.SBS

Rounded Rectangle: Writing System Settings

·	Applications ought to be storing data that isn’t user-specific in:

		HKEY_LOCAL_MACHINE\SOFTWARE\
<Company Name>\<App Name>\<version>\

Sample Code

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.cogentlogic.jndi.winreg.WinregInitContextFactory");
env.put(Context.PROVIDER_URL, "winreg://localhost/HKEY_LOCAL_MACHINE\\SOFTWARE");
initctx = new InitialContext(env);
Context subctx = initctx.createSubcontext("Cogent Logic\\VPA\\1.0");

// Create Bindings:
subctx.bind("DeliveryPath", "\\\\BEAUTY\\VPA");                     // REG_SZ
subctx.rebind("SequenceNumber", new Integer(1234));                 // REG_DWORD
subctx.rebind("Tolerance", new Float(1234.567F));                   // REG_BINARY

// Read bindings
obj = subctx.lookup("DeliveryPath");
System.out.println("DeliveryPath: " + obj + "  (data type: " + obj.getClass() + ")");
obj = subctx.lookup("SequenceNumber");
System.out.println("SequenceNumber: " + obj + "  (data type: " + obj.getClass() + ")");
obj = subctx.lookup("Tolerance");
System.out.println("Tolerance: " + obj + "  (data type: " + obj.getClass() + ")");
subctx.close();
initctx.close();

Rounded Rectangle: Results

DeliveryPath: \\BEAUTY\VPA  (data type: class java.lang.String)
SequenceNumber: 1234  (data type: class java.lang.Integer)
Tolerance: 1234.567  (data type: class java.lang.Float)

Rounded Rectangle: Accessing User Settings

·	User settings are held in:

		HKEY_CURRENT_USER

also HKEY_USERS which holds:

.DEFAULT
other user hives

·	User settings cover:
§	Desktop and user-interface settings
§	Application preferences and other user-related details
§	Environment variables
§	Printers, mapped network drives, console settings, etc.

Rounded Rectangle:

Rounded Rectangle: Sample Code

Hashtable env = new Hashtable(5, 0.75f);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.cogentlogic.jndi.winreg.WinregInitContextFactory");
env.put(Context.PROVIDER_URL, "winreg:///HKEY_CURRENT_USER\\Volatile Environment");
initctx = new InitialContext(env);
System.out.println("User's LOGONSERVER: " + initctx.lookup("LOGONSERVER"));


User's LOGONSERVER: \\BEAUTY

Rounded Rectangle: Writing User Settings

·	Applications ought to be storing data that is user-specific in:

		HKEY_CURRENT_USER\SOFTWARE\
<Company Name>\<App Name>\<version>\


Sample Code

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.cogentlogic.jndi.winreg.WinregInitContextFactory");
env.put(Context.PROVIDER_URL, "winreg://localhost/HKEY_CURRENT_USER\\SOFTWARE");
initctx = new InitialContext(env);
Context subctx = initctx.createSubcontext("Cogent Logic\\VPA\\1.0");

// Create Bindings:
subctx.rebind("EmailAddress", emailAddr);         // REG_SZ
subctx.bind("PreferredFont", font);			  // REG_BINARY