Tribal Knowledge Hub

Technical discoveries, solutions and musings from a long time IT professional.

Invoking WLST commands from Java

leave a comment

bea_logoI have been working with WebLogic Server since 1999 when it was still a BEA Systems product. Though professionally I  would characterize myself as an architect/developer, I have been working with WebLogic administrators for more than a decade. In fact, at BEA Systems I was the author of the original BEA WebLogic Administrator Certification exam. As a WebLogic consultant, the primary tools that I use with clients are the WebLogic Scripting Tool (WLST) and the WebLogic Diagnostic Framework (WLDF).

WLST is based on Jython, the all-Java version of Python. Since Jython runs in the JVM it is possible to invoke WLST commands directly form Java code. The possibilities are endless, you could use a Swing Gui front end and a WLST back end to provide automated management and monitoring for WebLogic.

Here is a simple example of WLST commands invoked from Java code. This code example uses a Runtime MBean to check how long the server has been running.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.IOException;
import java.io.OutputStream;

// from weblogic.jar
import org.python.core.PyLong;
import org.python.util.InteractiveInterpreter;
import weblogic.management.scripting.utils.WLSTInterpreter;

public class WSTLClient
{
  public static void main(String[] args)
  {
    InteractiveInterpreter interpreter = new WLSTInterpreter();    

    // WLST output to dev/null
    interpreter.setOut(new NullOutputStream());

    // Run commands no results
    interpreter.exec("connect('user','password','t3://host:port')");
    interpreter.exec("serverRuntime()");

    // Run command with result
    PyLong result = (PyLong)interpreter.eval("cmo.getActivationTime()");

    // calculate uptime
    long startTime = result.getValue().longValue();    
    long currentTime = System.currentTimeMillis();
    long uptimeMilli = currentTime - startTime;
    long uptimeMins = (uptimeMilli / 1000L) / 60L;

    System.out.println( "Server Uptime: " + uptimeMins + "min");
  }

  // custom class to iplement noop output stream
  public static class NullOutputStream extends OutputStream
  {
    @Override public void write(int b) throws IOException {}
    @Override public void write(byte[] b) throws IOException {}
    @Override public void write(byte[] b, int off, int len) throws IOException { }
  }
}

WLST documentation can be found here and here

here
. Feel free to use this code as the basis for your own scripts.

Written by Dave

May 9th, 2013 at 10:56 pm

Idempotent Services in SOA

leave a comment

leave a comment

Written by Dave

April 9th, 2013 at 9:16 am

Posted in SOA

A Simple jQuery Image Rotater

leave a comment

Recently I was looking to add a collection of rotating images to my website, so I searched for an image rotater using jQuery. I found a number of very sophisticated libraries designed to rotate images, create slideshows and display fancy transitions. Any one of these would have met my needs. What I did not find was a simple bare-bonesjQuery example that I could use on my web site and enhance as I saw fit. So, I decided to build one form scratch. This example is simple enough that anyone with any jQuery experience at all can understand it. Hopefully, the explanation is clear enough that anyone can incorporate it into their own web site or use it as a starting point to learn jQuery.

Because this this blog is built on  a framework that already uses jQuery, the image rotater can’s be directly embedded in this post. You can access an example by clicking on the image or at http://www.dchung.com/test/rotate.

Let’s look at the source code. The Javascript first then the HTML5:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<script src="scripts/jquery/1.8.3/jquery.min.js"></script>
<script>  

  // define array of image paths (src) and captions (figcaption)
  var images = [
      {src:"lighthouse.jpg", figcaption:"Peggy's Cove, Nova Scotia"},
      {src:"newyork.jpg",    figcaption:"New York before 9/11"},
      {src:"tetons.jpg",     figcaption:"Mormon Row, the Tetons"},
      {src:"tuileries.jpg",  figcaption:"Tuileries Garden, Paris"}
  ];

  // execute this anonymous function when the DOM has been loaded in memory
  $(document).ready(

    function() {

      // index to select item in array
      var i=0;

      // use the jQuery function ($) to select the <img> and <figcaption> by id
      var img = $('#rotate-img');
      var fig = $('#rotate-caption')

      // set the initial src and figcaption values 
      img.attr("src", images[i].src);
      fig.html(images[i].figcaption);

      // advance to next image 
      i++;

      // setInterval takes to parameters, a callback and an interval 
      window.setInterval(function() {

          // reset the index to 0 if past the end
          if( i >= images.length ) { 
            i = 0; 
          }

          // hide the current image then fade the new image in and set
          // the new caption 
          img.hide();
          img.attr("src", images[i].src).fadeIn('slow');
          fig.html(images[i].figcaption);      

          // advance to next image
          ++i;          
        },

        // set the image time to 5000ms
        5000 
      )
    }

  );
</script>

 

Notice that this example uses the ever-present closure. Notice that the values: i, img and fig are defined in the ready() function. The values  are used in the anonymous function called by the setInterval() function. The setInterval() function is called by the ready() function. By the time the anonymous function is called, both ready() and setInterval() have completed. The fact that the anonymous function can still access i, img and fig is a result of the closure.

 

60
61
62
63
<figure>
  <img id="rotate-img" width="250px" alt=""/>
  <figcaption id="rotate-caption"></figcaption>
</figure>

 

The associated HTML5 is shown above. Notice that the src attribute and the <figcaption> values are specified in the jQuery not the HTML5.

Feel free to use this code on your own site or use it as a starting point. There are several other features that could be added like different transitions, radomization … and more.

Written by Dave

December 10th, 2012 at 12:17 am

Posted in JavaScript,jQuery

Getting ‘Closure’

one comment

I have been a Java developer since 1995, before that I worked in C++.  So my language background is primarily statically-typed and object oriented. In the last several years, I’ve been involved in a number of projects that use JavaScript. I’ve come to recognize that JavaScript is a very powerful language in its own right — but it is dramatically different from the languages that I am used to. One of the features of JavaScript that was a mystery to me is closure.

Closure has no direct equivalent in my native language Java so it took a great deal of reading and experimentation for me to come to grips with this concept. The simplest description I can give is this.

In a closure a variable declared in an outer function is visible to an inner function even if the inner function’s lifecycle is longer than that of the outer function.

Here is an example using the Apache Cordova (PhoneGap) API. The phone Cordova API allows you to package HTML5/CSS/JavaScript applications natively for a number of mobile platforms. It also provides a JavaScript API to wrap many of the native device features like the accelerometer. This example shows a closure.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function startWatch() {

  var previousReading = { x: null, y: null, z: null };

  navigator.accelerometer.watchAcceleration(

    function (acceleration) {
      // use previousReading value here
    },
    onError, 
    { frequency: 500 }
 );

}

 

In this example the startWatch() function declares a variable previousReading and sets its x, y and z members to null. The three values will be used to represent acceleration in meters per second squared. The function then calls navigator.accelerometer.watchAcceleration(), passing in an anonymous callback for success. The anonymous callback defined on line 7 in the source code will be called at a rate of once every 500ms and it will compare the current acceleration with previousReading then update previousReading with the current acceleration.

The code is relatively straightforward except for one important detail previousReading was declared in the scope of the outer function startWatch. But the anonymous inner function (on line 7) will be called repeatedly even after the outer function is out of scope! What happens here is that the inner function keeps a reference to any values in the outer scope that it will need when it is invoked.

For those of you who are Java programmers, don’t think you are immune from this discussion. Closures are on the proposed feature list for JDK 8.

Written by Dave

November 3rd, 2012 at 5:01 pm

Posted in JavaScript

Cordova!

leave a comment

This week Apache Cordova (formerly known as PhoneGap) graduated from the Apache incubator and is now a top level Apache project. Cordova got its start as PhoneGap at a company called Nitobi. Adobe acquired Nitobi in 2011 and donated PhoneGap to the Apache Software Foundation which placed in incubator status. Adobe continues to develop PhoneGap as a release of Cordova but indicates that it may acquire some proprietary features.

The Cordova API allows developers to take applications built in HTML5, CSS3 and JavaScript and deploy them natively on a number of mobile platforms. Cordova sits as an abstraction layer on top of the device OS so that by using a different version of Cordova, the same app can run on Blackberry, iOS, Android, Windows Mobile and a host of other mobile platforms. Cordova includes a rendering engine for HTML and CSS as well as a JavaScript runtime. So Cordova applications run ‘natively’ and do not require a web browser.

I first encountered Cordova while working on an Android application for a defense contractor. The application was map focused and allowed users to see where friendly forces (and others if known) were on the battlefield. The application was built natively in Android using Java (Dalvik). A number of potential customers expressed an interest in the application but wanted it on different platforms like iOS or on a desktop browser.We were struggling with the potential of having to rewrite the application for multiple platforms including some proprietary hardware/software used in our industry.

After attending FOSSGEO in Denver, I decided to try a web-based solution using HTML5, CSS3, JavaScript and Sencha’s extJS JavaScript library. At the time, I built the application targeted to the browser. In doing some research I stumbled upon PhoneGap in a matter of hours I was able to demonstrate our application running on Android, iOS and in a desktop browser. As a strategy we were considering porting PhoneGap ourselves to our proprietary platforms and using it as the framework upon which to build a variety of cross platform apps.

In addition to packaging the web functionality, Cordova exposes many native device features including sensors, the file system and contacts. Since Cordova runs as a native app on the device, Cordova-based apps have been accepted in Apple’s iTunes app store. Here is a sampling of Cordova/PhoneGap based apps currently in production:

  • Wikipedia — Wikipedia Mobile
  • Facebook — Mobile SDK
  • SalesForce.com — Mobile Development SDK
  • IBM — Worklight Platform for Mobile
  • Microsoft — Halo Waypoint

As a mobile developer I think Cordova (I still have trouble not calling it PhoneGap) is a credible cross platform solution that can help organizations manage the growing array of mobile platforms in use by their customers and employees.

Written by Dave

October 23rd, 2012 at 2:55 pm

Posted in Mobile

Bumper Sticker

leave a comment

leave a comment

I was driving today and I saw a car with this bumper sticker. My wife was with me so I had to explain to her why I thought it was funny. Like all good humor, this bumper sticker is rooted in truth. In my job, I have had the opportunity to work in a wide range of industries. It is an almost universal truth, that as a practitioners, we do a poor job documenting our code.

Why don’t we (and I do mean we)  document our code?

Sometimes it is just laziness. I know that when I write code and it works, I consider the task done. I often view documentation as a separate task, a burden.

Often we as project leaders, fail to plan and budget for documentation. Doing it right takes work.

In some cases, it is precisely this sentiment. Since the code is the truth why should I document it? In fact one of the problems with documentation is that it must be maintained, just like the code. The only thing worse than no documentation is bad documentation.

Written by Dave

October 20th, 2012 at 8:31 pm

Posted in The Craft

Tribal Knowledge Hub

leave a comment

I have been blogging for years using primarily using both hosted TypePad and WordPress platforms. I finally decided to put some of my technical expertise to use. All of my existing blogs (and this new one) have been ported to the open source WordPress platform running on the the LAMP platform. LAMP stands for Linus, Apache, MySQL and Perl, PHP, Python. I have used all of these technologies in the past so it seemed to make sense to use them for my blog. All of these are hosted on a virtual private server at GoDaddy.com.

In this blog I will share and categorize some of the technical discoveries I make on the job and in my private exploration. Among the topics I will cover in this blog will be the issues encountered in configuring WordPress. My goal is to record and share so that when others encounter the same issues, they can find the solutions, just as I have on other technical blogs.

Written by Dave

October 19th, 2012 at 6:16 am

Posted in WordPress