Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Packaging, Resources and Components It is transportable The standard way to package Java is the jar-file. JAR stands for Java Archive. It supports hierarchical directories A jar-file is a container for sourcefiles, classfiles and other files. Technically a jar-file is a zip-file with some special files included. Selfdescribing via a manifest file. Packaging, Resources and Components 2 February 2005 Allows more efficient transferring Compressed 1 Packaging, Resources and Components 2 February 2005 2 The manifest is a file in the catalogue META-INF. It is a text file that describes the files in the archive jar-files are created with the jar tool. It works mostly like the UNIX tar tool. To create one you do JAR-files can be signed which means that it has been signed with a private key. To read such a file you need access to the corresponding public key. This is to certify the creator of the file. jar cf <jar-output-file> <class-files...> eg jar cf Myjar.jar ClassA.class ClassB.class To extract something you can do jar xf <jar-file> <class-file> Packaging, Resources and Components 2 February 2005 3 Packaging, Resources and Components 2 February 2005 4 The class loader can handle jar-files, this means that all standard packages are delivered as jar-files. They need not be unpacked. An important feature in modern programming is component technique. A component is a reusable piece of software with a well-defined behavior. It is somewhat equivalent to electronic components that have a well-defined interface and that can be combined to make some complex item. If you find a faster or cheaper component with the same interface you just replaces the old one. Packaging, Resources and Components 2 February 2005 5 Packaging, Resources and Components 2 February 2005 6 The Java component is the Bean. A Bean is no extension of some Beanclass or an implementaion of some Bean-interface. It is just one or more classes that adheres to a certain protocol. Some requirements on a Bean: Must be Serializable Must have a parameterless constructor Naming conventions of the instance variables and other resources Getter and setter method for resources. Packaging, Resources and Components 2 February 2005 7 Packaging, Resources and Components 2 February 2005 8 How Java Beans work What should be made into a Bean? They must be Serializable, this is because they might be saved into some object database. All classes can be made into a Bean but the most important ones are those that have a good chance to be reused by others. This includes utility classes that needs to be customized etc. They must have a standard constructor to be instansiable The resources and variables should be named xyz. There should be getter methods named as getXyz and setters called setXyz. Tools that works with Beans uses a special technique called introspection. Introspection can be used to inspect a component to find out its methods, variables and other resources. Packaging, Resources and Components 2 February 2005 9 Packaging, Resources and Components 2 February 2005 10 A simple Bean might look like Compile it with import java.awt.*; import java.io.Serializable; javac SimpleBean.java public class SimpleBean extends Canvas implements Serializable { Create a manifest file with the content private Color color = Color.green; Name: SimpleBean.class Java-Bean: True public SimpleBean() { setSize(60,40); setBackground(Color.red); } public Color getColor() { return color; } Create a jar file to be used in the Bean tool. public void setColor(Color newColor) { color = newColor; repaint(); } jar cfm SimpleBean.jar manifest.tmp SimpleBean.class public void paint(Graphics g) { g.setColor(color); g.fillRect(20, 5, 20, 30); } } All Bean requirements are fulfilled by this class. Packaging, Resources and Components 2 February 2005 11 Packaging, Resources and Components 2 February 2005 12 Internationalization (i18n) and localiation (l10n) However to use formatting of items like dates, numbers etc, we have to use the capabilities of Java. An example of simple multilanguage page can be There is support in webcontainer to generate text in different languages. Modern browsers can also display many languages. Packaging, Resources and Components 2 February 2005 13 Packaging, Resources and Components 2 February 2005 14 A simple servlet that produces this is package serv; import import import import import javax.servlet.*; javax.servlet.http.*; java.io.*; java.text.*; java.util.*; public class Serv extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Locale locale; DateFormat full; response.setContentType( "text/plain;charset=UTF-8"); PrintWriter out = response.getWriter(); locale = new Locale("en", "US"); full = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); out.println("In English:"); out.println("Hello World"); out.println(full.format(new Date())); out.println(); locale = new Locale("es", ""); full = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); out.println("En Espa\u00f1ol:"); out.println("\u00a1Hola Mundo!"); out.println(full.format(new Date())); out.println(); locale = new Locale("ja", ""); full = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); out.println("In Japanese:"); out.println("\u4eca\u65e5\u306f\u4e16\u754c"); out.println(full.format(new Date())); out.println(); Packaging, Resources and Components 2 February 2005 15 Packaging, Resources and Components 2 February 2005 16 locale = new Locale("zh", ""); full = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); out.println("In Chinese:"); out.println("\u4f60\u597d\u4e16\u754c"); out.println(full.format(new Date())); out.println(); locale = new Locale("ko", ""); full = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); out.println("In Korean:"); out.println( "\uc548\ub155\ud558\uc138\uc694\uc138\uacc4"); out.println(full.format(new Date())); out.println(); Some problems with internationalization: Character encoding. How to select language How to implement multilingual applications. locale = new Locale("ru", ""); full = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); out.println("In Russian (Cyrillic):"); out.print( "\u0417\u0434\u0440\u0430\u0432\u0441\u0442"); out.println( "\u0432\u0443\u0439,\u041c\u0438\u0440"); out.println(full.format(new Date())); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } } Packaging, Resources and Components 2 February 2005 17 Packaging, Resources and Components 2 February 2005 18 Most modern browsers supports the UTF-8 character set. This means that almost any language can be coded. Language detection: • • Inquire, ask the user about preferred language HTTP accept-language and accept-charset headers String prefLang = request.getHeader( “accept-language”); String prefCharset = request.getHeader( “accept-charset”); • Packaging, Resources and Components 2 February 2005 19 URL-encoding, encode the preferred language in the URL Packaging, Resources and Components 2 February 2005 20 Multilingual applications: • • Separate pages for each language Separate the content from the formatting. Use resource bundles and/or style-sheets (eg XSL) Packaging, Resources and Components 2 February 2005 21 A Resource Bundle is a collection of resources or resource names. Given a bundle, and a name of a resource you can get the value of that resource. There is an abstract class ResourceBundle that have a static method getBundle that can obtain the value of a given resource Packaging, Resources and Components 2 February 2005 22 There are two kinds of resource bundles The name of a subclass should be <bundle-name>_<locale>, eg MyResources_sv_SE. A property file uses the same convention but ending in .properties. A subclass of ResourceBundle, according to certain conventions A resource.properties file according to certain naming convention. For example you can have two files MessageBundle_en_US.properties MessageBundle_fr_FR.properties Packaging, Resources and Components 2 February 2005 23 Packaging, Resources and Components 2 February 2005 24 They contain Then you can use it as this greetings = Hello farewell = Goodbye inquiry = How are you? Locale currentLocale; ResourceBundle messages; currentLocale = new Locale( language, country); resp greetings = Bonjour farewell = Au revoir inquiry = Comment allez-vous? messages = ResourceBundle.getBundle( “MessageBundle”, currentlocale); System.out.println( messages.getString(“greetings”)); Packaging, Resources and Components 2 February 2005 25 Packaging, Resources and Components 2 February 2005 26 A more static way to do the same thing is to use a class instead. public class MessageBundle_en_US extends ListResourceBundle { // according to the protocol public Object [][] getContents() { return contents; } You can then do ResourceBundle messages = ResourceBundle.getBundle( “MessageBundle”, currentLocale); System.out.println( messages.getString(“greetings”)); private static Object [][] contents = { {“greetings” , “Hello”}, {“farewell” = “Goodbye”}, {“inquiry” = “How are you?”} }; } Packaging, Resources and Components 2 February 2005 27 Packaging, Resources and Components 2 February 2005 28 JSTL do contain i18n Tags. We will discuss JSTL later but: <%@ taglib uri=”http://java.sun.com/jsp/jstl/fmt” prefix=”fmt”%> <fmt:bundle basename=”Messagebundle”> <html> <head> <title> <fmt:message key=”greetings”/> </title> .... </fmt:bundle> Packaging, Resources and Components 2 February 2005 29