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

Sunday, June 7, 2015

Use Java library in C# - IKVM.NET

[1] IKVM.NET
[2] http://en.wikipedia.org/wiki/IKVM.NET

From [2]:

IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. IKVM is free software, distributed under a permissive free software licence.[1]

IKVM.NET includes the following components:

    A Java Virtual Machine implemented in .NET
    A .NET implementation of the Java class libraries
    A tool that translates Java bytecode (JAR files) to .NET IL (DLLs or EXE files).
    Tools that enable Java and .NET interoperability

With IKVM.NET you can run compiled Java code (bytecode) directly on Microsoft .NET or Mono. The bytecode is converted on the fly to CIL and executed.


Therefore if you want to use a java Jar library as C# assembly, you can use IKVM for the conversion.

This is great.

Tuesday, April 21, 2015

Create daemon in Java

This can be a windows service or a unix daemon. The implementation might be different depending on platform. So I'm starting to do some research.


1. Apache Commons Daemon [1]

Apache Commons Daemon is a good alternative. It has Procrun [2] for windows services, and Jsvc [3] for unix daemons. It uses less restrictive Apache license, and Apache Tomcat uses it as a part of itself to run on Windows and Linux! To get it work is a bit tricky, but there is an exhaustive article [4] with working example. With Apache Commons Daemon you can now have a custom executable name and icon! You can also get a custom Windows tray monitor with your own name and icon!

Jsvc is a set of libraries and applications for making Java applications run on UNIX more easily.
Jsvc allows the application (e.g. Tomcat) to perform some privileged operations as root (e.g. bind to a port < 1024), and then switch identity to a non-privileged user.It can run on Win32 via the Cygwin emulation layer, however Win32 users may prefer to use procrun instead.

Procrun is a set of applications that allow Windows users to wrap (mostly) Java applications (e.g. Tomcat) as a Windows service. The service can be set to automatically start when the machine boots and will continue to run with no user logged onto the machine.


2. Java Service Wrapper (JSW) [download]

See short introduction in [5]. Also an implementation of it: YAJSW - Yet Another Java Service Wrapper [6]. Cons (?): community version cannot be run on a server.


References:

[1] http://commons.apache.org/daemon/
[2] http://commons.apache.org/daemon/procrun.html
[3] http://commons.apache.org/daemon/jsvc.html
[4] Java as Windows Service with Apache Commons Daemon
[5] Running Java Applications as a Windows Service
[6] YAJSW - Yet Another Java Service Wrapper 
[7] http://stackoverflow.com/questions/68113/how-to-create-a-windows-service-from-java-app
[8] Creating a Java Daemon (System Service) for Debian using Apache Commons Jsvc


Saturday, April 4, 2015

Some readings today

Here are links to some readings today:

- Java equals()和hashCode()

   Q: 怎样实现正确的equals()方法
   A: 首先,我们需要遵守Java API文档中equals()方法的约定,如下:
    自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true。
    对称性:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 true。
    传递性:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 应返回 true。
    一致性:对于任何非空引用值 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,前提是对象上 equals 比较中所用的信息没有被修改。

    对于任何非空引用值 x,x.equals(null) 都应返回 false。
    其次,当我们重写equals()方法时 , 不同类型的属性比较方式不同,如下:
    属性是Object类型, 包括集合: 使用equals()方法。
    属性是类型安全的枚举: 使用equals()方法或==运算符(在这种情况下,它们是相同的)。
    属性是可能为空的Object类型: 使用==运算符和equals()方法。
    属性是数组类型: 使用Arrays.equals()方法。
    属性是除float和double之外的基本类型: 使用==运算符。
    属性是float: 使用Float.floatToIntBits方法转化成int,然后使用 ==运算符。
    属性是double: 使用Double.doubleToLongBits方法转化成long , 然后使用==运算符。

  Q: 重写equals()方法为什么一定要重写hashCode()方法
  A: 如果不这样做就会违反Java API中Object类的hashCode()方法的约定,从而导致该类无法很好的用于基于散列的数据结构(HashSet、HashMap、Hashtable、LinkedHashSet、LinkedHashMap等等)。i.e., when search in hashtable based data structure, it'll first find the bucket based on hashCode(), then do compare by equals() to find exact match. Object.hashCode() by default is different for every object. This can't be used when, say, 2 strings "aaa" and "aaa", are different objects so have different hashCode, then you cannot find "aaa" in a container that contains "aaa".


- 揭秘阿里服务互联网金融的关系数据库——OceanBase
  This lacks details though.

JS前台加密,java后台解密实现
   This is not hard to decode though since JS is open.





Saturday, July 5, 2014

JavaEE and SSH (Struts/Spring/Hibernate)

Java Structs/Spring/Hibernate are used widely to created JEE applications.

Some resources are here:

- A self-contained Java Struts Spring Hibernate Tutorial [1] in pdf to download. About 80 pages.

- mkyong.com [2] has many tutorials. E.g., Struts 2 + Spring + Hibernate Integration Example

- AppFuse [3] is a full-stack framework for building web applications on the JVM. It can generate SSH website template in SSH. This is a demo site of it.

The JEE ecology system is large, complex and distributed. It contains a lot of technology and can build highly professional enterprise web applications. However the distributed nature decides that it is poor to manage. For example, for each new functionality, one needs to go to a website, download another Jar and include it into the lib. This is comparable to adding references in VS.NET. In VS.NET all references come in package out of the box, and is much easier to handle mentally and practically. Another example is the directory structure. JEE directory structure is much more convoluted than ASP.NET, which artificially adds difficulty to the understandability.


== Servlet and JSP ==

Basically, servlet is more primitive, can use different protocols, embed html in java code. JSP is layered above servlet. JSP uses HTTP protocol, embed java code in html.  JSP is compiled into Servlet class automatically by container server (e.g. Tomcat).  JSP is more modern, like ASP and PHP, has better separation of java logic from UI presentation.

See more at [4][5].

== Tomcat ==

Tomcat is an application server for Java servlet.

It is possible to use Tomcat independent from Apache. But Tomcat works only for Java Servlet, and does not have as many other functions as Apache.

This is what happens when Tomcat works together with Apache. When Apache receives a browser request as a long stream of bytes, it passes it to Tomcat, which creates a request object to parse the stream into request parameters, and passes the request object to a (servlet) java class. After the (servlet) java class finishes the processing, and returns a response object, it then interprets this into a http response stream and passes the stream to Apache to be sent back.

To configure Tomcat to work with Apache, a Tomcat-Apache connector module is needed. This is often the mod_jk module using Apache JServ Protocol (AJP). By default Apache uses port 80, Tomcat uses 8080, the mod_jk module uses port 8009.

See more at [6][7][8].

== Development with Eclipse ==

Tomcat uses port 8080. If development and production are on the same machine, as in the case of personal project, Eclipse can do development in a separate Tomcat instance, the port number can be configured in server.xml, e.g., to 8081. At deployment time, it can use the export function (similar to Android project being exported to a apk file) to package the project into a *.war file. The *.war file, when deployed to the Tomcat server in production, is unpacked (you can do this manually with: jar -xvf *.war) and being served. This way, development and production use different Tomcat instances and different ports on the same machine.

To know if a port is being used by any process, say port 8080, type this:
sudo lsof -i :8080
To kill such process and release the port graciously [9], do:
sudo kill -15 <pid>
Here the tomcat pid can be obtained by:
ps -ax | grep tomcat

When running Tomcat with Eclipse, the server path used by Eclipse (similar to wwwroot for IIS, or /var/www/html for Apache, or /var/lib/tomcat7/webapps/ for Tomcat7) is:  workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps

== Issues running Eclipse with Tomcat ==

My feeing with Eclipse JEE development is, many configurations can go wrong. There is no central support, so you just get on google and search for other people's discussions and solutions. The quality of environment integration is less mature as the .NET world. For example:

1) after first creating a project, it may complain the j2se1.4.0 JRE not match installation jdk. So you have to go to Library, and reselect the jdk to use.
2) another example, Tomcat7 installation has one directory at /var/lib/tomcat7/, and another at /usr/share/tomcat7. The latter needs references to many files/folders in the former location, so you need to manually create soft links in the latter place.
3) add a server to the current project may not work, and complains port 8080 already in use, for this you need to change the port number used in server.xml.  Solutions to these problems are found online, e.g., in [9][10][11][12].
4) as one more prominent example, I finished the tutorial project in [1], in the process I need to download lib jar files from 15 different websites to set it up.
5) use the log4j project as an example, in version 1.2, one file log4j-1.2.17.jar can do it. But in version 2.0-rc2, you got a large number of jar files to choose from, for different platforms at least, have to include the api and core jar files, then some others. It's getting way bloated up.

Compared to the IIS world, there are much to worry here, and many need manual setup.  The .metadata folder under the current workspace contains many files and is complicated.  If you kill the Eclipse process before it's properly shutdown, Eclipse may not be able to start, and fix is to remove something corrupted here in .metadata.

== Struts ==

A MVC web framework for JEE development. For more details see [13][14].

== Spring ==

A programming and configuration framework for JEE development. Its principles include IOC (Dependency Injection), AOP, MVC, Convention over configuration etc. It's ambitious. But criticisms say it's just a loose bundle of best-practices.

For more details see [15][16].

== Hibernate ==

An ORM module for JEE development. ORM stands for Object-Relational Mapping. It basically handles all the work connecting to database and hides the details, through the use of configuration files. For more details see [17][18].

Java SSH can be used together. One major reason of their appearing was to address the unnecessary complexity of EJB.

I feel it's too much building up of concepts, which are often old wine packaged in new bottles. Servlet and JSP are the most primitive building blocks of the entire stack. While these technologies aim to provide a framework that's supposed to make enterprise application development easy, they are actually creating layers over layers, and become very much counter-intuitive to pick up.  Tools are just tools, but now just to learn how to use the tools can become a full time job.  In this aspect, LAMP and .NET are much more intuitive.


References:

[1] Java Struts Spring Hibernate Tutorial
[2] mkyong.com - Java related tutorials
[3] AppFuse - a full-stack framework for building web applications on the JVM

[4] Difference of JSP and Servlet
[5] Coding Servlet and JSP from ground up
[6] Can Tomcat run WITHOUT Apache
[7] Tomcat.apache.org: omcat-Apache Howto
[8] Apache 2 with Tomcat 6: How to configure Tomcat to work with Apache

[9] Tomcat startup 8080 port already in use
[10] Eclipse cannot load the tomcat server configuration at serverstomcat-v7-0-server-at-localhost-config
[12] Cannot add a new Tomcat server

[13] struts.apache.org
[14] Wiki: Apache Struts
[15] spring.io
[16] Wiki: Spring Framework
[17] hibernate.org
[18] Wiki: Hibernate (Java)


Monday, January 13, 2014

Java web development review

Just want to review some basic java web development technologies, such as JSP, JSTL and servlets.

Online search on topic "JSP Tutorial" 

Below tutorials are listed in the order of from simple to more complex:

JSP Tutorial - very very basic tutorial
Tutorials Point: Basic JSP Tutorial - a little more detailed
Servlet
 
Building Web Apps in Java: Beginning & Intermediate Servlet & JSP Tutorials
The Java EE 5 tutorial





Thursday, September 16, 2010

Book: Apache Jakarta and Beyond

Book: Apache Jakarta and Beyond - A Java programmer's introduction. By Larne Pekowsky. ISBN 0-321-23771-4 QA76.73.J38P44 2004. 2005.

This book introduces a series of Jakarta tools to be used by Java programmers. Including:

- Ant
- Eclipse
- Testing with JUnit
- Testing web sites with HTTPUnit
- Further web testing with Jakarta Cactus
- Stress Testing with Jakarta JMeter
- Simplifying Bean Development with BeanUtils
- Traversing Hierarchical Data with JXPath
- Database tools:
Hsqldb, DBCP, OJB
- Logging
- Java.util.logging
- Log4j
- Configuring program options
- Jakarta CLI (Command-Line Interface)
- Jakarta Digester (XML-based: object stack, element matching patterns, processing rules)
- Working with Text 1: Regular Expressions
- Working with Text 2: Searching
- Creating office documents with POI
- Scripting
- Tomcat
- The standard tag library
- Struts: application toolkit/web application framework
- Cocoon: provides a complete XML-based publishing suite, for the generation, manipulation and rendering of XML.

Wednesday, August 18, 2010

JEE

It's been some time since I used J2EE in class. Well it's called JEE nowadays. Today reviewed JEE tools.
Tutorials: 
- Spring Tutorial. (Took about 5 hours to finish, spreaded into 3 days)
- Hibernate Tutorial.
- Maven Tutorial. (Took half an hour to finish)
- Another Maven Tutorial.
- Oracle's JEE 5 tutorial

The environment is:
- OS: Windows XP
- Java: 1.60
- Ant: 1.7 (http://ant.apache.org/manual/index.html)
[Need to setup environment variables ANT_HOME and Java_HOME]
- Apache Tomcat: 6.0.29 (http://tomcat.apache.org/download-60.cgi#6.0.29)
Install as a service. (32-bit/64-bit Windows Service Installer). Use port 8080.
Local site: http://localhost:8080
Homepage root is: $CATALINA_HOME/webapps/ROOT/index.html
$CATALINA_HOME is installation site of Tomcat.
- Spring: 2.5.0
Most recent version is at http://www.springsource.org/download
But I need version 2.5.0, which can be downloaded from
https://olex.openlogic.com/packages/spring
This contains many other utilities. The most recent version package does not.
- Maven: 2.2.1. Obtained here. [Need to set up M2_HOME]
- Eclipse: Java EE IDE for web developers. (http://www.eclipse.org/downloads/)

A little note:
- Step 5.5 code for JdbcProductDaoTests.java should include the following two imports to function:
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
import springapp.domain.Product;
Here the first imported class is in library spring-test.jar.
If eclipse can't find this then manually add it to properties->Java Build Path->Libraries.

Some concepts:
- SSH: Struts, Spring, Hibernate.
- EJB3.0+struts2+iBATIS
- jsp+Servlet+jdbc is the basics for all Java frameworks.
- jQuery: light-weighted javascript framework.
- Dojo, extjs: javascript frameworks. Used with JSF.
- SOA: Service Oriented Application
- Apache lucene: text search engine library written in Java
- Nutch: Open-source web-search software, built on Lucene Java.
- Webwork: Java web-application development framework.
- NoSQL
- Local search. e.g. foursquare

Some thoughts so far:
- A lot of work for simple application in Spring.
- JEE tools apply software engineering concepts, that may be the reason
it's said to be suitable for large web application development. On the
other hand this makes it heavy-weighted. Setup environment and getting
familiar with the tools cost lots of time. On this, .NET, PHP, ASP are
easier to catch up.
- Eclipse eats big memory (over 200MB).
- Maven is like Ant.

Blog Archive

Followers