Archive for the 'Java' Category

 

Creating a wlfullclient.jar for JDK 1.6 client applications..

Jul 30, 2010 in Java, Weblogic

From http://download.oracle.com/docs/cd/E12840_01/wls/docs103/client/jarbuilder.html

Use the following steps to create a wlfullclient.jar file for a JDK 1.6 client application:

1.Change directories to the server/lib directory.

cd WL_HOME/server/lib

2. Use the following command to create wlfullclient.jar in the server/lib directory:

java -jar wljarbuilder.jar

3.You can now copy and bundle the wlfullclient.jar with client applications.

4.Add the wlfullclient.jar to the client application’s classpath.

EJB container interface..

Jun 07, 2010 in EJB, JBoss, Weblogic

  1. ejbCreate, ejbPostCreate
  2. ejbLoad, ejbStore
  3. ejbActivate, ejbPassivate

Reversing a singly-linked list in Java ..

Dec 27, 2009 in Algorithms, Java

From http://www.informatics.susx.ac.uk/courses/dats/notes/html/node46.html

public void reverse(ListNode head) {
    ListNode current = head;
    head = null;
    ListNode save = null;
    while (current != null) {
        save = current;
        current = current.next;
        save.next = head;
        head = save;
    }
}

Breadth-first traversal algorithm..

Dec 02, 2009 in Algorithms, Java

http://en.allexperts.com/q/Computer-Science-3197/Bread-First-Search-Algorithm.htm

BFS(G, s)
 for each vertex u in V - { s } do
   u.color = white
   u.dist = infinity  // ''dist'' is distance from s
   u.pred = nil       // ''pred'' is predecessor
 s.color = gray
 s.dist = 0
 s.pred = nil
 Q = { s }            // ''Q'' is a FIFO queue
 while Q not empty do
   u = Q.head         // get element on top of queue
   for each v in neighbors(u) do  // visit every node u has an edge to
     if v.color == white then
       v.color = gray
       v.dist = u.dist + 1
       v.pred = u
       Q.enqueue(v)   // add v to back of queue
   Q.dequeue          // pop u off top of queue
   u.color = black

And a bit more Java-ish..


BFS(root) {
  cn = root;

  s.push(cn.getValue()); // Save node value on stack
  cn.setValue(-1); // mark node visited
  q.enqueue(cn);

  while(! q.notEmpty() ) {
    cn = q.dequeue();
    foreach v in cn.getChildren() {
      if (v.getValue() != -1) {
        // This node not visited yet
        s.push(v.getValue()); // save node value on stack
        v.setValue(-1); // mark node visited
        q.enqueue(v);
      }
    }
  }

}

And even simpler..


BFS(root) {
  cn = root;

  cn.visit(); // mark node visited
  q.enqueue(cn);

  while(! q.notEmpty() ) {
    cn = q.dequeue();

    foreach v in cn.getChildren() {
      if ( ! v.visited() ) {
        // This node not visited yet
        v.visit(); // mark node visited
        q.enqueue(v);
      }
    }
  }

}

Quick Eclipse Shortcuts ..

Oct 28, 2009 in Eclipse, Uncategorized

From http://www.ibm.com/developerworks/opensource/library/os-eclipse-master3/index.html

  • – Open Type - Ctrl+Shift+T
  • – Pick part of a string - select section, press Ctrl+1
  • – Quick Outline - Ctrl+O
  • – Enable ‘Breadcrumbs Bar’ - Alt+Shift+B
  • – Code formatting - highlight, Ctrl+Shift+F
  • – Watch the ‘Overview Ruler’ for errors or warnings

From the “Workbench User Guide” ..

History Navigate > Backward (Alt+Left Arrow)

Various Functions > Ctrl+3

Editor List > Ctrl+E

Global Find/Replace > Ctrl+H

Customize, View Keys / Shortcuts > Menu / General / Keys

Find a string incrementally > Edit / Incremental Find Next (Ctlr+J)
> or Find Previous (Ctrl+Shift+J)

Go to last edit location > Navigate / Go to last edit location (Ctrl+Q)

Manipulating lines > Insert new line above (Ctrl+Shift+Enter) or below (Ctrl+Enter)
> Convert to lowercase (Ctrl+Shift+Y) or uppercase (Ctrl+Shift+X)

Quick Diff (what changed while editing) > General / Editors / Text Editors / Quick Diff (Ctrl+Shift+Q)

Next / previous navigation > next = Ctrl+. , previous = Ctrl+, (plus comma)

Word completion > Alt+/. (Ctrl+. on the Mac)

Tomcat performance monitoring..

Aug 27, 2009 in Java, Tomcat

From http://www.devx.com/Java/Article/32730

  • Tomcat Resource Pools.

    Connector Thread Pool

    Database Connection Pool

  • Use Tomcat Valves for implementing lightweight monitoring w/o JMX.

Hash table notes..

Aug 26, 2009 in Engineering, Java, JavaUsage, Software

From http://techinterviewcoach.com/blog/?p=30#more-30

  • Hash tables are backed by arrays.
  • A hash function is used to map the keys to an index within the backing array.
  • Put, Get, Delete operations all run in constant ( O(1) ) time.
  • Hash function determines performance.
  • Even with the best hash function, collisions can occur.
  • Main advantage of HTs over other data structures is speed.
  • Growing HTs need to be rehashed but the cost is amortized by constant cost of ‘put’.
  • Should not use HTs for small data sets, but everybody does.
  • Rehashing should occur when the load factor (occupied array slots / array size) reaches over .75.

Java example of basic cache (LRU) using LinkedHashMap ..

Aug 24, 2009 in Java, JavaUsage

From http://stackoverflow.com/questions/1229780/question-about-lru-cache-implementation-in-java

import java.util.Map;
import java.util.LinkedHashMap;

public class t1 {

    final int MAX_ENTRIES = 2;

    Map cache = new LinkedHashMap(MAX_ENTRIES+1, .75F, true) {
        // This method is called just after a new entry has been added
        public boolean removeEldestEntry(Map.Entry eldest) {
	    System.out.println("eldest: " + eldest.getKey() + "=" + eldest.getValue());
            return size() > MAX_ENTRIES;
        }
    };

    public void cacheAdd(String pkey, String pstr) {
        // Add to cache
        cache.put(pkey, pstr);
    }

    public static void main(String[] args) {
        t1 ot1 = new t1();
        ot1.cacheAdd(”k1″, “str1″);
        ot1.cacheAdd(”k2″, “str2″);
        ot1.cacheAdd(”k3″, “str3″);
        ot1.cacheAdd(”k4″, “str4″);
    }

    /* –
// Get object
Object o = cache.get(key);
if (o == null && !cache.containsKey(key)) {
    // Object not in cache. If null is not a possible value in the cache,
    // the call to cache.contains(key) is not needed
}

// If the cache is to be used by multiple threads,
// the cache must be wrapped with code to synchronize the methods
cache = (Map)Collections.synchronizedMap(cache);
– */

}

Also, see http://codeidol.com/java/javagenerics/Maps/Implementing-Map/

Notes on “Java Web Services” ..

Jul 02, 2009 in Books, Java, Software, WebServices

This book is a bit old.. 2002..




  • – SOA - Service Oriented Architecture
    – 3 major roles: Provider, Registry (Broker) and Requester.
    – SOA is based on web services.
  • – SOAP - Simple Object Access Protocol
    – 2 major methods: Message-based Document Exchange AND RPC
    (Remote Procedure Calls).
    – All structures based on XML.
    – A SOAP message contains an Envelope and a Header.
    – A SOAP message may contain (MIME) attachments.
    – Main transport protocol is HTTP(S).
    – Providers (Receivers) are usually based on Servlets.
  • – SOAP-RPC
    – Method signatures contain a single SOAP structure.
    – SOAP service methods must match info in deployment descriptor.
    – Errors and Faults: VersionMismatch, MustUnderstand, DTDNotSupported, etc..
  • – WSDL - Web Services Description Language
    – WSDL is an XML grammar for describing a web service
    as a collection of access end-points capable of exchanging
    messages in a procedure- or document-oriented fashion (p. 72)
    – The reasonable flow is to create service methods and
    than generate WSDL from code using tools.
    – Best practices: web service is (functionally) coarse-grained
    and messages are more business-oriented than programmatic.
  • – UDDI - Universal Description, Discovery and Integration
    – Similar to an Internet search engine for businesses.
    – UBR - UDDI Business Registry (the Public Cloud).
    – Designed for B2B mostly.
    – 2 APIs: inquiry and publishing API.
    – JAXR = Java API for XML Registries.
    – Some details: Categorization (NAICS, ISO, etc..),
    Identifier (DUNS, Thomas Reg, etc),
    tModel (web service metadata)
  • – JAX-RPC and JAXM
    – JAXM = Java API for XML Messaging.
    – JAX-RPC = Java API for XML-based RPC.
    – JAXM may be used as a frontend to SOAP-based messaging
    frameworks through the use of “profiles”.
    – JAX-RPC covers code generation for client and server parts,
    dynamic SOAP, creating services within J2EE and J2SE environments,
    bindings, serialization, mapping of datatypes between WSDL and Java,
    etc..
  • – The Java Web Service (JWS) Standard
    – Proposed by BEA, using templates to create simple web services.
    – Example (HelloWorld.jws <- note the .jws extension)
    Note the annotations @operation, @conversation ..

    import com.bea.jws.*
    
    public class HelloWorld extends Service {
      /**
      * @operation
      * @conversation stateless
      */
      public String getHelloWorld() {
        return "Hello, World!";
      }
    }
    

    Other values for @conversation are ’start’, ‘continue’ or
    ‘finish’. The ’start’ directive, for example, starts a session.

  • – Security notes
    – Use of SSL (HTTPS), encryption, signing and secure hashing.
    – Security Assertion Markup Language (SAML) - used with Single Sign-On (SSO).
  • – Resources
    http://aws.amazon.com/
    http://seekda.com/
    http://www.programmableweb.com/
    http://www.webservices.org/
    http://xmethods.com/

Colophon - European (Alpine) Ibex - a wild goat.

Java pattern matching example..

Jun 19, 2009 in Java, JavaUsage

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class TestPattern {
    public static void main(String[] args) {
	String pin = “1234567″;
	String pinPattern = “[0-9]+”;
	String result = null;

	Pattern pattern = Pattern.compile(pinPattern);
	Matcher matcher = pattern.matcher(pin);

	result = “”;
	while(matcher.find()) {
	    result += matcher.group();
	}
	if (result.equals(pin)) {
	    result = “PIN accepted”;
	}
	else {
	    result = “PIN invalid”;
	}
	System.out.println(result);
    }
}