Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, January 13, 2013

Setting the class path


Synopsis

The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either the -classpath option when calling a JDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.
http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html

Tuesday, January 8, 2013

Maven in 5 Minutes


Building a Project

The vast majority of Maven-built projects can be built with the following command:
mvn clean install
This command tells Maven to build all the modules, and to install it in the local repository. The local repository is created in your home directory (or alternative location that you created it), and is the location that all downloaded binaries and the projects you built are stored.
That's it! If you look in the target subdirectory, you should find the build output and the final library or application that was being built.
Note: Some projects have multiple modules, so the library or application you are looking for may be in a module subdirectory.
While this will build most projects and Maven encourages this standard convention, builds can be customisable. If this does not suffice, please consult the project's documentation.

More than just the Build

Maven can do more than just build software - it can assist with testing, run web applications and produce reports on projects, as well as any number of other tasks provided by plug-ins.



http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

http://blog.csdn.net/totogogo/article/details/1933659

Youtube Maven Tutorial
http://www.youtube.com/watch?v=5Tp3XCEfMGk&hd=1&width=720&height=405&iframe=true&__utma=246996102.1158874441.1357686362.1357686362.1357686362.1&__utmb=246996102.2.10.1357686362&__utmc=246996102&__utmx=-&__utmz=246996102.1357686362.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)&__utmv=-&__utmk=74590609


Building a Project with Maven



maven术语
http://blog.csdn.net/totogogo/article/details/1933718


Repository:仓库用于存放artifact,它可以是本地仓库,也可以是远程仓库。Maven有一个默认的远程仓库--central,可以从http://www.ibiblio.org/maven/下载其中的artifact。在Windows平台上,本地仓库的默认地址是C:/Documents and Settings/USER_NAME/.m2/repository。
我的理解:在执行mvn时,maven会check maven remote central repository,如果local repository没有相关的POM and jar或者版本旧于remote central repository,就会从remote central repository下载它们到local repository。

    Project
:任何您想build的事物,Maven都可以认为它们是工程。这些工程被定义为工程对象模型(POM,Poject Object Model)。一个工程可以依赖其它的工程;一个工程也可以由多个子工程构成。

    POM
:POM(pom.xml)是Maven的核心文件,它是指示Maven如何工作的元数据文件,类似于Ant中的build.xml文件。POM文件位于每个工程的根目录中。   
Artifact:你的project通过maven生成的东东叫作artifact(project生成的jar就是一个典型的artifact)。需要被使用(依赖)的artifact都要放在仓库(见Repository)中,否则Maven无法找到 (识别)它们。
 Dependency:为了能够build或运行,一个典型的Java工程会依赖其它的包。在Maven中,这些被依赖的包就被称为dependency。dependency一般是其它工程的artifact。
   
Plug-in:Maven是由插件组织的,它的每一个功能都是由插件提供的。插件提供goal(类似于Ant中的target),并根据在POM中找到的元数据去完成工作。主要的Maven插件要是由Java写成的,但它也支持用Beanshell或Ant脚本写成的插件。   
Snapshot:工程中可以(也应该)有一个特殊版本,它的版本号包括SNAPSHOT字样。该版本可以告诉Maven,该工程正处于开发阶段,会经常更新(但还未发布)。当其它工程使用此类型版本的artifact时,Maven会在仓库中寻找该artifact的最新版本,并自动下载、使用该最新版。


What’s POM?
pom.xml是maven的核心文件,是maven用来build project的configuration file, 就象Ant的build.xml。for most project,缺省的pom.xml包含了一些default value,通常情况下你不需要在你的pom.xml里设置这些值。例如:
  • 缺省的build directory是“target”目录
  • 缺省的source directory是“src/main/java”目录
  • 缺省的build directory是“src/main/test”目录
pom.xml的configuration info包括:
  • Project基本信息,如project version, description, developers, mailing lists
  • project dependencies
  • 要执行的plugins or goals
  • build profiles
下面参考我的第一个maven命令生成的pom.xml来讲解它的配置信息
<project xmlns=。。。。>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany.app</groupId>
 <artifactId>my-app</artifactId>
 <packaging>jar</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>Maven Quick Start Archetype</name>
 <url>http://maven.apache.org</url>
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 </dependencies>
</project>
project root element of pom.xml files.
modelVersion设置POM version, 类似于设置project version,目前好像maven 2只允许该值为4.0.0
groupId设置project的organization or group 的ID,它是project的唯一识别之一(另1个是artifactId)。groupId通常以full domain name作为属性值,例如所有maven plugins的groupId的属性值为org.apache.maven.plugins
artifactId设置当前project将生成的primary artifact的unique base name(请注意:是base name,而不是name,因为artifact name是由”<artifactId>-<version>”组成,例如myapp-1.0.jar)。
packaging设置package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.)。该属性值不仅是用来设置被生成的artifact是JAR, WAR还是EAR,还用来设置package artifact时要process的lifecycle,即JAR, WAR, EAR的build lifecycle是不同的。packaging的缺省属性值为JAR。如果你的project要package成jar,你不需要设置该属性
version设置project生成的artifact的version。Maven能帮你进行version management。如果你看到该属性值里包含有“SNAPSHOT”string,则表示该project处于开发阶段,而不是发布阶段。
name设置project name,该属性值通常用于Maven's generated documentation.
url设置该project的web site url. 该属性值通常用于Maven's generated documentation
description设置project description该属性值通常用于Maven's generated documentation
Super POM
Super POM是Maven's default POM,它设置了maven的基本default value(例如build directory为“target”)。它的所有设置会被你的project的pom.xml继承,你如果要覆盖缺省设置,就需要进行显式设置。
下面是super pom content:
<project>
 <modelVersion>4.0.0</modelVersion>
 <name>Maven Default Project</name>
 <repositories>
    <repository>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <layout>default</layout>
      <url>http://repo1.maven.org/maven2</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
 </repositories>
 <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Maven Plugin Repository</name>
      <url>http://repo1.maven.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
 </pluginRepositories>
 <build>
    <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <finalName>${pom.artifactId}-${pom.version}</finalName>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    <sourceDirectory>src/main/java</sourceDirectory>
    <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>src/test/resources</directory>
      </testResource>
    </testResources>
 </build>
 <reporting>
    <outputDirectory>target/site</outputDirectory>
 </reporting>
</project>
Minimal POM
一个pom.xml必须要有的东东是:
  • project root
  • modelVersion - should be set to 4.0.0
  • groupId - the id of the project's group.
  • artifactId - the id of the artifact (project)
  • version - the version of the artifact under the specified group
例:
<project>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany.app</groupId>
 <artifactId>my-app</artifactId>
 <version>1</version>
</project>
其中的groupId, artifactId and version组成了project的fully qualified artifact name:<groupId>:<artifactId>:<version>,例如上例的fully artifact name为"com.mycompany.app:my-app:1"
在minimal pom里,没有设置<packaging>,那么会使用缺省值:jar。
也没有设置<repositories>。如果没有repositories属性,就会继承super pom的repositories配置,maven在执行这个minimal pom时,就会按从缺省的repositories:http://repo1.maven.org/maven2 下载相关的dependencies。
Project Inheritance
Project inheritance是child project继承parent project!
pom.xml中能被合并(merge)的elements包括:
  • dependencies
  • developers and contributors
  • plugin lists (including reports)
  • plugin executions with matching ids
  • plugin configuration
  • resources
Super pom就是project inheritance最典型的例子。你也可以通过在child project pom.xml里设置<parent> element,从而实现继承parent pom。
The Scenario
我们使用之前的那个artifact, com.mycompany.app:my-app:1. 同时我们还有另外一个artifact, com.mycompany.app:my-module:1. my-module project的pom.xml为:
<project>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany.app</groupId>
 <artifactId>my-module</artifactId>
 <version>1</version>
</project>
然后我们看看这2个artifact的目录结构
.
 |-- my-module
 |   `-- pom.xml
 `-- pom.xml
注意: my-module/pom.xml is the POM of com.mycompany.app:my-module:1 while pom.xml is the POM of com.mycompany.app:my-app:1
The Solution
如果我们想让com.mycompany.app:my-app:1成为com.mycompany.app:my-module:1的parent artifact,我们就需要将com.mycompany.app:my-module:1's POM修改成:
com.mycompany.app:my-module:1's POM
<project>
 <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany.app</groupId>
 <artifactId>my-module</artifactId>
 <version>1</version>
</project>
通过<parent> element,可以设置pom的parent pom,从而从parent pom里继承一些配置。
另外,如果你想the module的groupId and version设置成和parent一样,那么你可以去掉child pom的groupId and version element。如
<project>
 <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 <artifactId>my-module</artifactId>
</project>
这样就会继承parent pom的groupId and version
Example 2
The Scenario
Example 1能够正常运行的先决条件是:parent project已经install to local repository或者parent project pom.xml必须恰好处于child project pom.xml的上一级目录。
而如果parent project没有install to repository或者象下列结构,使用example 1的代码是不行的:
.
 |-- my-module
 |   `-- pom.xml
 `-- parent
     `-- pom.xml
The Solution
对于上面的目录结构(或者其他不属于example 1的结构),child pom.xml需要添加<relativePath> element来设置你的parent project的path。如:
<project>
 <parent>
    <groupId>com.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1</version>
    <relativePath>../parent/pom.xml</relativePath>
 </parent>
 <modelVersion>4.0.0</modelVersion>
 <artifactId>my-module</artifactId>
</project>
Project Aggregation
Project aggregation是child project委托parent project执行。它和project inheritance很相似,project inheritance是在child pom.xml里设置parent project,而Project aggregation则是在parent pom.xml里设置child project。,
Child project是通过在parent pom.xml里定义<module>来设置Project Aggregation的。Project Aggregation意味着在执行parent project时,也会同时执行定义了aggregation的child project。
另外还有一点要注意:设置了project aggregation的parent project pom.xml必须把<packaging>的值设置为“pom”
Example 3
The Scenario
依然是使用example 1的parent project and child project。
com.mycompany.app:my-app:1's POM
<project>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany.app</groupId>
 <artifactId>my-app</artifactId>
 <version>1</version>
</project>
com.mycompany.app:my-module:1's POM
<project>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany.app</groupId>
 <artifactId>my-module</artifactId>
 <version>1</version>
</project>
directory structure
.
 |-- my-module
 |   `-- pom.xml
 `-- pom.xml
The Solution
如果我们要把my-module委托给my-app,我们需要修改parent pom.xml如下
<project>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany.app</groupId>
 <artifactId>my-app</artifactId>
 <version>1</version>
 <packaging>pom</packaging>
 <modules>
    <module>my-module</module>
 </modules>
</project>
Example 4
The Scenario
Example31能够正常运行的先决条件是:child project已经install to local repository或者parent project pom.xml必须恰好处于child project pom.xml的上一级目录。
而如果child project没有install to repository或者象下列结构,使用example 3的代码是不行的:
.
 |-- my-module
 |   `-- pom.xml
 `-- parent
     `-- pom.xml
The Solution
类似于example 3,但是设置child project的path不同。
<project>
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.mycompany.app</groupId>
 <artifactId>my-app</artifactId>
 <version>1</version>
 <packaging>pom</packaging>
 <modules>
    <module>../my-module</module>
 </modules>
</project>
Project Inheritance VS Project Aggregation
使用Project Inheritance的情况:如果你有多个maven projects, 而且它们有一些相同的配置,那么你就应该refactor你的projects,把这些相同的配置从projects中提取出来,然后把这些相同配置制作成一个parent project,然后你所有的projects都继承该parent project。Super POM就是最典型的project inheritance例子
使用Project Aggregation的情况:如果你有一组projects常常需要同时进行maven build的操作,那么你就可以创建一个parent project,然后把这组projects作为该parent project的modules,这样你只需要maven parent project即可执行这组projects。一个典型的例子就是:project A是build jar,project B是package war,该war会把project A的jar纳入, project C是package ear,该ear会把project B的war纳入
当然,你可以同时使用Project Inheritance and Project Aggregation,即在parent project里设置modules,同时在child projects里设置parent project。


Monday, January 7, 2013

RMI - FTP - Active and Passive FTP

java.rmi.server
Class UnicastRemoteObject

java.lang.Object
  extended by java.rmi.server.RemoteObject
      extended by java.rmi.server.RemoteServer
          extended by java.rmi.server.UnicastRemoteObject
http://docs.oracle.com/javase/1.5.0/docs/api/java/rmi/server/UnicastRemoteObject.html

rmi tutorial
http://www.slideshare.net/junyuo/a-short-java-rmi-tutorial
http://www.youtube.com/watch?v=ILeAeFZOkMI

java.net
Class InetAddress

java.lang.Object
  extended by java.net.InetAddress 
http://docs.oracle.com/javase/1.5.0/docs/api/java/net/InetAddress.html
 

The InetAddress Class
The InetAddress class is a utility class that represents an IP address, which is either a 32-bit or 128-bit unsigned number used by Internet Protocol (IP). The protocols such as Transmission Control Protocol (TCP), User Datagram Protocol (UDP), and Stream Control Transport Protocol (SCTP) are built on top of IP. The instance of this class consists of an IP address and possibly its corresponding host name. The class converts numeric addresses to host names, and vice versa. The other networking classes that you have studied previously, such as Socket and ServerSocket, use this class for identifying hosts.
The class does not have any public constructors, which means you cannot create any arbitrary addresses. All created addresses must be checked with DNS (Domain Name System). To create an InetAddress object, you must use one of the available factory methods. ThegetLocalHost factory method returns the InetAddress object that represents the local host. The getByName method accepts the host name as its parameter and returns the corresponding InetAddress object. Both methods throw an UnknownHostException if they are unable to resolve the host name. The getAllByName factory method returns an array of InetAddress objects that represent all the addresses the given name resolves to—note that DNS mappings allow you to associate a single name with a set of machines (IP addresses). This method may throw anUnknownException like the other two methods if it cannot resolve the name to at least one address.
The class provides various getter methods that return the host name, address, and so on. The isMulticastAddress method checks whether theInetAddress is an IP multicast address. Inet4Address and Inet6Address are the two direct, known subclasses of this class. As the name indicates, Inet4Address represents an IP version 4 address and Inet6Address represents an IP version 6 address. For so many years, we have been using IPv4 addresses that consist of four parts—each being a byte of data. The IPv6 address consists of eight 16-bit pieces and will soon be in use as the Internet keeps growing.
We have used a variation of the class InetSocketAddress in our earlier examples while connecting to a remote server. The simple example given in Listing 20-9 illustrates the use of this class.
image
Listing 20-9   DNS Resolution Application
image
 
image
In the main method, we call the getAllByName static method of the InetAddress class. The method takes a host as the parameter and returns an array of the associated IP addresses. First, we list out all servers hosted by Google and then by CNN. At the end, we list out the IP address of our local machine. When you run this application, you will see output similar to the following (note that by the time you run this code, Google/CNN might have changed/added a few servers):
image
image
From this output, you can see that Google has hosted their website on five different IPs, whereas CNN hosts their website on six known servers. The output shows that the local machine has only one IP associated with it (which, of course, goes without saying).

Managed File Transfer and Network Solutions

http://www.jscape.com/blog/bid/80512/Active-v-s-Passive-FTP-Simplified
When a client experiences problems when connecting to your FTP server, one thing you might want to look into is whether you've set your FTP data transfer mode to either active or passive. 
Active and passive are two possible modes that an FTP connection can operate on. Taking into consideration the network configurations and security controls in place, you should choose one mode over the other.
But before we discuss which mode is best for what scenario, let's first talk about the basics of these two modes, which can best be explained if we start our discussion with the two channels an FTP session normally has.

FTP command channel and data channel

A typical FTP session operates using two channels: a command (or control) channel and a data channel. As their names imply, the command channel is used for transmitting commands as well as replies to those commands, while the data channel is used for transferring data. 
Unless you configure your FTP server differently, you will normally set your command channel to use port 21. The port you'll use for the data channel, on the other hand, can differ depending on which data transfer mode you choose. If you choose active mode, then the data channel will normally be port 20. But if you choose passive mode, then the port that will be used will be a random port. 
FTP command and data channels resized 600
Note that the ports we are referring to here up to this point are only the ports on the server side. We'll include client-side ports in our discussion in a short while.

Active mode FTP

Among the two modes, Active mode is the older one. It was the mode introduced in the early days of computing when mainframes were more common and attacks to information security were not as prevalent. 
Here's a simplified explanation on how an active mode connection is carried out, summarized in two steps. Some relevant steps (e.g. ACK replies) have been omitted to simplify things.
  1. A user connects from a random port on a file transfer client to port 21 on the server. It sends the PORT command, specifying what client-side port the server should connect to. This port will be used later on for the data channel and is different from the port used in this step for the command channel.
  2. The server connects from port 20 to the client port designated for the data channel. Once connection is established, file transfers are then made through these client and server ports.
ftp active mode resized 600

Passive mode FTP

In passive mode, the client still initiates a command channel connection to the server. However, instead of sending the PORT command, it sends the PASV command, which is basically a request for a server port to connect to for data transmission. When the FTP server replies, it indicates what port number it has opened for the ensuing data transfer. 
Here's how passive mode works in a nutshell:
  1. The client connects from a random port to port 21 on the server and issues the PASV command. The server replies, indicating which (random) port it has opened for data transfer. 
  2. The client connects from another random port to the random port specified in the server's response. Once connection is established, data transfers are made through these client and server ports.
ftp passive mode resized 600

Active mode vs Passive mode - which is more suitable for you?

There's a reason why I opted to simplify those two diagrams above. I wanted to focus on the main difference between active mode and passive mode FTP data transfers. If you compare those two diagrams, one of the things that should really stand out are the opposing directions at which thesecond arrows (which also represent the data channels) are pointing to.
In this section, we'll focus on those second arrows and the ports associated with them.
In the active mode, the second arrow is pointing to the client. Meaning, the client initially specifies which client-side port it has opened up for the data channel, and the server initiates the connection. 
By contrast, in the passive mode, the second arrow is pointing to the server. Here, the server specifies which server-side port the client should connect to and the client initiates the connection.
There shouldn't be any problem had there not been any firewalls in existence. But threats to information security are on the rise and hence the presence of firewalls is almost always a given. In most cases, clients are located behind a firewall or a NAT (which basically functions like a firewall). In such cases, only a select number of predefined ports are going to be accessible from the outside. 
Remember that in an active mode configuration, the server will attempt to connect to a randomclient-side port. So chances are, that port wouldn't be one of those predefined ports. As a result, an attempt to connect to it will be blocked by the firewall and no connection will be established.
active ftp with firewall resized 600 
In this particular scenario, a passive configuration will not pose a problem. That's because the client will be the one initiating the connection, something that a client-side firewall won't have any problem with.
passive ftp with firewall resized 600
Of course, it's possible for the server side to have a firewall too. However, since the server is expected to receive a greater number of connection requests compared to a client, then it would be but logical for the server admin to adapt to the situation and open up a selection of ports to satisfy passive mode configurations.

Security considerations when setting up passive FTP

As explained earlier, if you're administering an FTP server, it would be best for you to configure your server to support passive mode FTP. However, you should bear in mind that in doing so, you would be making your system more vulnerable to attacks. Remember that, in passive mode, clients are supposed to connect to random server ports.
Thus, to support this mode, not only should your server have to have multiple ports available, your firewall should also allow connections to all those ports to pass through!
But then the more open ports you have, the more there will be to exploit. To mitigate the risks, a good solution would be to specify a range of ports on your server and then to allow only that range of ports on your firewall.

Where to set up passive port range in JSCAPE MFT Server

For those of you who are already using JSCAPE MFT Server, you can specify a range of ports for your passive mode FTP connections by going to Services > FTP/S > Passive port range in your JSCAPE MFT Server Manager. Because low ports (particularly those < 1024) are reserved, choose a high port range (i.e. large numbers). For example, from 5000 to 6000. For better security, don't just copy the example. Use your own!
passive port range resized 600
In the event that the IP address your server uses in responding to requests for passive connections is not routable via the Internet, you'll need to enter your public IP address in the Passive IP field. 
JSCAPE MFT Server is a file transfer solution that supports more than just FTP. You can take a free trial now.