Archive for April, 2007

Now that we’ve gone to the trouble of (Freelance web design)

Friday, April 27th, 2007

Now that we’ve gone to the trouble of creating a policy file, let’s use it. You can tell the default security manager to use the policy file with another command-line option to the java interpreter: C:> java -Djava.security.manager - Djava.security.policy=EvilEmpire.policy EvilEmpire Connected! EvilEmpire can now make its socket connection because we have explicitly granted it permission with a policy file. The default security manager still protects us in other ways, however; EvilEmpire cannot write or read files on the disk except in the directory it came from; it cannot make connections to any other network addresses except the one we specified. Take a moment and bask in this warm fuzzy feeling. Later, in Chapter 20, you’ll see policytool again when we explain signed applets. In this chapter, codebases are identified by URLs, which isn’t the most secure option. Through tricky network shenanigans, a clever forger may be able to give you code that appears to be from somewhere it’s not. Crytpographically signed code is even more trustworthy; see Chapter 20 for the full details. 3.3 The Class Path The concept of a path should be familiar to anyone who has worked on a DOS or Unix platform. It’s an environment variable that provides an application with a list of places to look for some resource. The most common example is a path for executable programs. In a Unix shell, the PATH environment variable is a colon-separated list of directories that are searched, in order, when the user types the name of a command. The Java CLASSPATH environment variable, similarly, is a list of locations that can be searched for packages containing Java class files. Both the Java interpreter and the Java compiler use CLASSPATH when searching for packages and classes on the local host. A location on the class path can be a directory name or the name of a class archive file. Java supports archives of class files in its own Java archive ( JAR) format, and in the conventional ZIP format. JAR and ZIP are really the same format, but JAR archives include extra files that describe each archive’s contents. JAR files are created with the SDK’s jar utility; many tools for creating ZIP archives are publicly available. The archive format enables large groups of classes to be distributed in a single file; the Java interpreter automatically extracts individual class files from an archive, as needed. The precise means and format for setting the class path vary from system to system. On a Unix system, you set the CLASSPATH environment variable with a colon-separated list of directories and class archive files: CLASSPATH=/home/vicky/Java/classes:/home/josh/oldstuff/foo.zip:. On a Windows system, the CLASSPATH environment variable is set with a semicolon-separated list of directories and class archive files: set CLASSPATH=D:usersvickyJavaclasses;. The first example above, for Unix, specifies a class path with three locations: a directory in the user’s home, a ZIP file in another user’s directory, and the current directory, which is always specified with a dot (.). The last component of the class path, the current directory, is useful
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services

First, fill in the (Make a web site) codebase with the URL

Friday, April 27th, 2007

First, fill in the codebase with the URL of the directory containing EvilEmpire as shown in the figure. Then click on Add Permission. Yet another window pops up, shown in Figure 3.4. Figure 3.4. Creating a new permission Choose SocketPermission from the first combo box. Then fill out the second text field on the right side with the network address that EvilEmpire will connect to. Finally, choose connect from the third combo box. Click on OK; you should see the new permission in the policy entry window, as shown in Figure 3.3. Click on Done to finish creating the policy. Then choose Save As from the File menu and save the policy file as something memorable, like EvilEmpire.policy. You can quit policytool now; we’re all done with it. There’s nothing magical about the policy file you just created. Take a look at it with a text editor. It has a simple syntax; here’s the important part, showing the policy we just created: grant codeBase “file:/c:/Projects/Exploring/” { permission java.net.SocketPermission “207.46.131.13″, “connect”; }; You can eschew policytool entirely and just create policy files with a text editor, if you’re more comfortable that way. 3.2.3 Using a Policy File with the Default Security Manager
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

C:> java -Djava.security.manager EvilEmpire SecurityException: could not connect. (Free web hosting music)

Friday, April 27th, 2007

C:> java -Djava.security.manager EvilEmpire SecurityException: could not connect. C:> That’s better, but suppose that the application actually has a legitimate reason to make its network connection. We’d like to leave the default security manager in place, just to be safe, but we’d like to grant this application permission to make a network connection. 3.2.2 The policytool Utility To permit our EvilEmpire example to make a network connection, we need to create a policy file that contains the appropriate permission. A handy utility called policytool, included in SDK 1.2 and later, helps you make policy files. Fire it up from a command line like this: C:> policytool You may get an error message when policytool starts up about not finding a default policy file. Don’t worry about this; just click OK to make the message go away. We want to add a network permission for the EvilEmpire application. The application is identified by its origin, also called a codebase . A codebase is described by a URL. In this case, it will be a file: URL that points to the location of the EvilEmpire application on your disk. If you started up policytool, you should be looking at its main window, shown in Figure 3.2. Click on Add Policy Entry. Another window pops up, like the one shown in Figure 3.3 (but with the fields empty). Figure 3.2. The policytool window Figure 3.3. Adding a policy entry
Note: If you are looking for high quality webhost to host and run your jsp application check Vision christian web host services

3.2 Policy Files Java 2 provides a simple (Web hosting domain names)

Thursday, April 26th, 2007

3.2 Policy Files Java 2 provides a simple mechanism for protecting your computer from evil programs like viruses. If you download a program from somewhere on the Internet, how can you prevent it from stealing information on your computer and sending it back out into the Internet? How can you prevent a malicious program from disabling your computer or erasing data on your disk? Most computing platforms have no answer for these questions. Java 2 offers powerful ways to limit the actions of running code. Before Java 2, much of the buzz about security had to do with the security of applets. The applet ran with security restrictions that prevented the applet from doing questionable things like reading from or writing to the disk or contacting arbitrary computers on the network. In Java 2, it’s just as easy to apply applet-style security to applications. Furthermore, it’s easy to fine-tune the access you allow applications. For example, you can allow an application to access the disk, but only in a specific directory, or you can allow network access to certain addresses. Why is this important? Let’s suppose that you need a certain application, like a calendar or an address manager. You go to your favorite Internet search engine and find a promising-looking Java application that does just what you want. You download and run it. But it’s entirely possible that what you’ve downloaded is not what you wanted. It could be a computer virus that infects your computer. Or it could simply be a malicious program that erases files from your disk. In this case, it would have been a really good idea to restrict the application’s actions. 3.2.1 The Default Security Manager You can use an option of the java interpreter to install a default security manager. This security manager enforces many of the same rules as for applets. To see how this works, let’s write a little program that does something questionable, making a network connection to some computer on the Internet. (We’ll cover the specifics of network programming later, in Chapter 11 and Chapter 12.) //file: EvilEmpire.java import java.net.*; public class EvilEmpire { public static void main(String[] args) throws Exception{ try { Socket s = new Socket(”207.46.131.13″, 80); System.out.println(”Connected!”); } catch (SecurityException e) { System.out.println(”SecurityException: could not connect.”); } } } If you just run this program with the Java interpreter, it will make the network connection: C:> java EvilEmpire Connected! C:> This is kind of scary. Let’s install the default security manager, like this:
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services

% java test The interpreter searches for the (Web site counters)

Thursday, April 26th, 2007

% java test The interpreter searches for the class in the class path, a list of directories where packages of classes are stored. We’ll discuss the class path in detail in the next section. The class path is typically specified by an environment variable, which you can override with the command-line option -classpath. After loading the class specified on the command line, the interpreter executes the class’s main( ) method. From there, the application can start additional threads, reference other classes, and create its user interface or other structures, as shown in Figure 3.1. Figure 3.1. Starting a Java application The main( ) method must have the right method signature. A method signature is a collection of information about the method, like a C prototype or a forward function declaration in other languages. It includes the method’s name, type, and visibility, as well as its arguments and return type. The main( ) method must be a public, static method that takes an array of String objects as its argument and does not return any value (void): public static void main ( String [] myArgs ) Because main( ) is a public and static method, it can be accessed directly from another class using the name of the class that contains it. We’ll discuss the implications of visibility modifiers such as public and the meaning of static in through Chapter 6. The main( ) method’s single argument, the array of String objects, holds the command-line arguments passed to the application. As in C, the name that we give the parameter doesn’t matter; only the type is important. Unlike C, the content of myArgs is a true array. There’s no need for an argument count parameter, because myArgs knows how many arguments it contains and can happily provide that information: int argc = myArgs.length; Java also differs from C in another respect here: myArgs[0] is the first command-line argument, not the name of the application. If you’re accustomed to parsing C command-line arguments, you’ll need to be careful not to trip over this difference. The Java interpreter continues to run until the main( )method of the initial class file has returned, and until any threads that it started are complete. Special threads designated as “daemon” threads are silently killed when the rest of the application has completed.
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

Chapter 3. Tools of the Trade You have (Web site construction)

Thursday, April 26th, 2007

Chapter 3. Tools of the Trade You have many options for Java development environments, from the traditional text-editor-andcommand- line environment to IDEs like WebGain’s Visual Caf , Inprise’s JBuilder, Tek-Tools’ KAWA, or Sun’s Forte for Java. The examples in this book were developed using the Solaris and Windows versions of the Java Software Development Kit (SDK), so we will describe those tools here. When we refer to the compiler or interpreter, we’ll be referring to the command-line versions of these tools, so the book is decidedly biased toward those of you who are working in a Unix or DOS-like environment with a shell and filesystem. However, the basic features we’ll be describing for Sun’s Java interpreter and compiler should be applicable to other Java environments as well. In this chapter, we’ll describe the tools you’ll need to compile and run Java applications. The last part of the chapter discusses how to pack Java class files into Java archives ( JAR files). Chapter 20, describes the ability to “sign” classes within a JAR file, and to give greater privileges to classes with a signature that you trust. 3.1 The Java Interpreter A Java interpreter is software that implements the Java virtual machine and runs Java applications. It can be a standalone application like the SDK’s java program, or part of a larger application like the Netscape Navigator web browser. It’s likely that the interpreter itself is written in a native, compiled language for your particular platform. Other tools, like Java compilers and development environments, can be written in Java (and should be, we’d argue, in order to maximize the portability of the Java development environment). Sun’s Forte for Java is one example of a pure-Java IDE. The Java interpreter performs all of the activities of the Java runtime system. It loads Java class files and interprets the compiled byte-code. It verifies compiled classes that are loaded from untrusted sources. In an implementation that supports dynamic, or just-in-time, compilation, the interpreter also serves as a specialized compiler that turns Java byte-code into native machine instructions. Throughout the rest of this book, we’ll be building both standalone Java programs and applets. Both are kinds of Java applications run by a Java interpreter. The difference is that a standalone Java application has all of its parts; it’s a complete program that runs independently. An applet is more like an embeddable program module. The Java interpreter can’t run an applet directly, because it is used as part of a larger application. To run an applet, you can use a web browser like Sun’s HotJava or Netscape Navigator, or the appletviewer tool that comes with the SDK. Both HotJava and appletviewer are standalone Java applications run directly by the Java interpreter; these programs implement the additional structure needed to run Java applets. Sun’s Java interpreter is called java. In a standalone Java application, one class includes a main( ) method, which contains the statements to be executed upon startup. To run the application, execute the interpreter, specifying that class as an argument. You can also specify options to the interpreter, as well as arguments to be passed to the application: % java [interpreter options ] class_name [program arguments ] The class should be specified as a fully qualified class name, including the package name, if any. Note, however, that you don’t include the .class file extension. Here are a few examples: % java animals.birds.BigBird
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

currentColor( ) methods, where there is the potential (Web site templates)

Wednesday, April 25th, 2007

currentColor( ) methods, where there is the potential for a more serious “out of bounds” error. The synchronized modifier tells Java to acquire a lock for the class that contains the method before executing that method. Only one method can have the lock on a class at any given time, which means that only one synchronized method in that class can be running at a time. This allows a method to alter data and leave it in a consistent state before a concurrently running method is allowed to access it. When the method is done, it releases the lock on the class. Unlike synchronization in other languages, the synchronized keyword in Java provides locking at the language level. This means there is no way that you can forget to unlock a class. Even if the method throws an exception or the thread is terminated, Java will release the lock. This feature makes programming with threads in Java much easier than in other languages. See Chapter 8 for more details on coordinating threads and shared data. Whew! Now it’s time to say goodbye to HelloJava. We hope that you have developed a feel for the major features of the Java language, and that this will help you as you go on to explore the details of programming with Java.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

handled. The section of code that receives the (Ftp web hosting)

Wednesday, April 25th, 2007

handled. The section of code that receives the exception object is said to catch the exception. An exception causes the execution of the instigating section of code to stop abruptly and transfers control to the code that receives the exception object. The try/catch construct allows you to catch exceptions for a section of code. If an exception is caused by any statement inside of a try clause, Java attempts to deliver the exception to the appropriate catch clause. A catch clause looks like a method declaration with one argument and no return type. If Java finds a catch clause with an argument type that matches the type of the exception, that catch clause is invoked. A try clause can have multiple catch clauses with different argument types; Java chooses the appropriate one in a way that is analogous to the selection of overloaded methods. You can catch multiple types of exceptions from a block of code. Depending on the type of exception thrown, the appropriate catch clause will be executed. If there is no try/catch clause surrounding the code, or a matching catch clause is not found, the exception is thrown up the call stack to the calling method. If the exception is not caught there, it’s thrown up another level, and so on until the exception is handled. This provides a very flexible error-handling mechanism, so that exceptions in deeply nested calls can bubble up to the surface of the call stack for handling. As a programmer, you need to know what exceptions a particular statement can generate, so methods in Java are required to declare the exceptions they can throw. If a method doesn’t handle an exception itself, it must specify that it can throw that exception, so that its calling method knows that it may have to handle it. See Chapter 4, for a complete discussion of exceptions and the try/catch clause. So, why do we need a try/catch clause in the run( ) method? What kind of exception can Thread’s sleep( ) method throw and why do we care about it, when we don’t seem to check for exceptions anywhere else? Under some circumstances, Thread’s sleep( ) method can throw an InterruptedException, indicating that it was interrupted by another thread. Since the run( ) method specified in the Runnable interface doesn’t declare it can throw an InterruptedException, we must catch it ourselves, or the compiler will complain. The try/catch statement in our example has an empty catch clause, which means that it handles the exception by ignoring it. In this case, our thread’s functionality is so simple it doesn’t matter if it’s interrupted. All of the other methods we have used either handle their own exceptions or throw only general-purpose exceptions that are assumed to be possible everywhere and don’t need to be explicitly declared. 2.4.7 A Word About Synchronization At any given time, there can be a number of threads running in the Java runtime system. Unless we explicitly coordinate them, these threads will be executing methods without any regard for what the other threads are doing. Problems can arise when these methods share the same data. If one method is changing the value of some variables at the same time that another method is reading these variables, it’s possible that the reading thread might catch things in the middle and get some variables with old values and some with new. Depending on the application, this situation could cause a critical error. In our HelloJava examples, both our paintComponent( ) and mouseDragged( ) methods access the messageX and messageY variables. Without knowing the implementation of our particular Java environment, we have to assume that these methods could conceivably be called by different threads and run concurrently. paintComponent( ) could be called while mouseDragged( ) is in the midst of updating messageX and messageY. At that point, the data is in an inconsistent state and if paintComponent( ) gets lucky, it could get the new x value with the old y value. Fortunately, in this case, we probably would not even notice if this were to happen in our application. We did, however, see another case, in our changeColor( ) and
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services

2.4.5 Running Code in the Thread Our run( (Web proxy server)

Wednesday, April 25th, 2007

2.4.5 Running Code in the Thread Our run( ) method does its job by setting the value of the variable blinkState. We have added blinkState, a boolean value, to represent whether we are currently blinking on or off: boolean blinkState; A setColor( ) call has been added to our paintComponent( ) method to handle blinking. When blinkState is true, the call to setColor( ) draws the text in the background color, making it disappear: g.setColor(blinkState ? getBackground() : currentColor( )); Here we are being somewhat terse, using the C-like ternary operator to return one of two alternative color values based on the value of blinkState. Finally, we come to the run( ) method itself: public void run( ) { try { while(true) { blinkState = !blinkState; repaint( ); Thread.sleep(500); } } catch (InterruptedException ie) {} } Basically, run( )is an infinite while loop. This means the method will run continuously until the thread is terminated by a call to the controlling Thread object’s interrupt( ) method. The body of the loop does three things on each pass: Flips the value of blinkState to its opposite value using the not operator, “!” Calls repaint( ) to redraw the text Sleeps for 500 milliseconds (half a second) sleep( ) is a static method of the Thread class. The method can be invoked from anywhere and has the effect of putting the current thread to sleep for the specified number of milliseconds. The effect here is to give us approximately two blinks per second. The try/catch construct, described in the next section, traps any errors in the call to the sleep( ) method of the Thread class. 2.4.6 Exceptions The try/catch statement in Java is used to handle special conditions called exceptions. An exception is a message that is sent, normally in response to an error, during the execution of a statement or a method. When an exceptional condition arises, an object is created that contains information about the particular problem or condition. Exceptions act somewhat like events. Java stops execution at the place where the exception occurred, and the exception object is said to be thrown by that section of code. Like an event, an exception must be delivered somewhere and
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services

Web proxy server - So how do we tell the thread which

Tuesday, April 24th, 2007

So how do we tell the thread which method to run? Well, the Thread object is rather picky; it always expects to execute a method called run( )to perform the action of the thread. The run( ) method can, however, with a little persuasion, be located in any class we desire. We specify the location of the run( ) method in one of two ways. First, the Thread class itself has a method called run( ). One way to execute some Java code in a separate thread is to subclass Thread and override its run( ) method to do our bidding. Invoking the start( ) method of the subclass object causes its run( ) method to execute in a separate thread. It’s not always desirable or possible to create a subclass of Thread to contain our run( ) method. The Thread class has a constructor that takes an object reference as its argument. If we create a Thread object using this constructor and call its start( ) method, the Thread executes the run( ) method of the argument object, rather than its own. In order to accomplish this, Java needs a guarantee that the object we are passing it does indeed contain a compatible run( ) method. We already know how to make such a guarantee: we use an interface. Java provides an interface named Runnable that must be implemented by any class that wants to become a Thread. 2.4.3 The Runnable Interface We’ve used the second technique in the HelloJava4 example. To create a thread, a HelloJava4 object passes itself (this) to the Thread constructor. This means that HelloJava4 itself must implement the Runnable interface, by implementing the run( ) method. This method is called automatically when the runtime system needs to start the thread. We indicate that the class implements the interface in our class declaration: public class HelloJava4 extends JComponent implements MouseMotionListener, ActionListener, Runnable {…} At compile time, the Java compiler checks to make sure we abide by this statement. We have carried through by adding an appropriate run( ) method to HelloJava4. It takes no arguments and returns no value. Our run( ) method accomplishes blinking by changing the color of our text a couple of times a second. It’s a very short routine, but we’re going to delay looking at it until we tie up some loose ends in dealing with the Thread itself. 2.4.4 Starting the Thread We want the blinking to begin when the application starts. So we’ll start the thread in the initialization code in HelloJava4’s constructor. It takes only two lines: Thread t = new Thread(this); t.start( ); First, the constructor creates a new instance of Thread , passing it the object that contains the run( ) method to the constructor. Since HelloJava4 itself contains our run( ) method, we pass the special variable this to the constructor. this always refers to our object. After creating the new Thread, we call its start( ) method to begin execution. This, in turn, invokes HelloJava4’s run( ) method in a separate thread.
Note: If you are looking for cheap webhost to host and run your apache application check Vision jboss web hosting services