Few questions about some Java code
Hello,
I've just posted a topic about the build.xml explanation but this topic is just going about some real java code/syntax questions.
I've following code (console.java):
Code:
package remote;
import interfaces.Page;
import interfaces.PageHome;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.util.Iterator;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import org.apache.log4j.BasicConfigurator;
public class Console {
public static void main(String[] args) throws IOException {
BasicConfigurator.configure();
Context ic = null;
PageHome ph = null;
try {
ic = new InitialContext();
ic.addToEnvironment("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
ic.addToEnvironment("java.naming.provider.url","jnp://localhost:1099");
ic.addToEnvironment("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
ph = (PageHome)PortableRemoteObject.narrow(ic.lookup("Page"), PageHome.class);
} catch (NamingException ne) {
System.err.println("Error in Naming : " + ne.getMessage());
System.exit(1);
}
Page pm = null;
try {
pm = ph.create();
} catch (RemoteException re) {
System.err.println("Error in network: " + re.getMessage());
System.exit(1);
} catch (Exception ce) {
System.err.println("Error creating bean: " + ce.getMessage());
ce.printStackTrace();
System.exit(1);
}
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
int ID = 0;
int menu = 0;
while(menu < 3)
{
System.out.println("Welcome to the CMS Deleter!");
System.out.println("Make your choice:");
System.out.println("1. List all pages");
System.out.println("2. Delete a page");
System.out.println("3. Quit");
menu = Integer.parseInt(br.readLine());
switch(menu)
{
case 1:
System.out.println("ID\tTitle");
System.out.println("--\t-----");
Iterator pi = pm.getAllPages().iterator();
while(pi.hasNext())
{
classes.Page p = (classes.Page)pi.next();
System.out.println(p.getID() + "\t" + p.getTitle());
}
break;
case 2:
System.out.println("Input ID of the page to delete:");
ID = Integer.parseInt(br.readLine());
try{
classes.Page del = (classes.Page)pm.getPageByID(ID);
pm.deletePage(del);
}
catch(Exception e)
{
System.out.println("ERROR: " + e.getMessage());
}
break;
default:
break;
}
}
}
}
Now i don't understand the following:
Code:
ic = new InitialContext(); ic.addToEnvironment("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
ic.addToEnvironment("java.naming.provider.url","jnp://localhost:1099"); ic.addToEnvironment ("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
ph = (PageHome)PortableRemoteObject.narrow(ic.lookup("Page"), PageHome.class);
- What are those " java.naming.*** " ?
- and what does the " (PortableRemoteObject) " do? Really don't understand that sentence...
- what does "System.exit(1); " do? I think shut down the application, but i'm not sure...
Code:
InputStreamReader isr = new InputStreamReader( System.in);
- What does system.in here?
Kind regards,
Pieter Maertens
ps: I know it (or I) might sound stupid, but tomorrow I have exam and those commands I never saw..... :(