Archive for May, 2008
Playing with Dates in Java
by Frank Kim on May.28, 2008, under Java SE
I always forget how to do these things so I thought I should write it down.
Changing Dates:
Here is an example of getting the date for seven days ago from now.
Calendar sevenDaysAgo = Calendar.getInstance();
sevenDaysAgo.add(Calendar.DATE, -7);
sevenDaysAgo.set(Calendar.HOUR_OF_DAY, 0);
sevenDaysAgo.set(Calendar.MINUTE, 0);
sevenDaysAgo.set(Calendar.SECOND, 0);
sevenDaysAgo.set(Calendar.MILLISECOND, 0);
Formatting Dates:
Here is the simple way to format a date and time.
DateFormat.getDateTimeInstance().format(sevenDaysAgo.getTime()))
The output is:
May 21, 2008 12:00:00 AM
Note that according to the JavaDoc, DateFormats are inherently unsafe for multithreaded use. You should not make a call to a static instance of a DateFormat nor share a single instance across thread boundaries without proper synchronization. The same applies to Calendar. For more information on this see Sun Bug #6231579 and Sun Bug #6178997.
Creating Dates:
Here is an example of creating a date, the birth date January 1, 1970.
Calendar birthDate = Calendar.getInstance();
birthDate.clear();
birthDate.set(Calendar.YEAR, 1970);
birthDate.set(Calendar.MONTH, 0);
birthDate.set(Calendar.DATE, 1);
For further reading please Create a Date object using the Calendar class and of course the JavaDocs for Calendar, Date and DateFormat.
Invalid column type SQLException with ATG Repository
by Frank Kim on May.28, 2008, under Repository
Recently I was seeing this exception.
CONTAINER:atg.repository.RepositoryException; SOURCE:java.sql.SQLException: Invalid column type
at atg.adapter.gsa.GSAItemDescriptor.executeQuery(GSAItemDescriptor.java:6682)
at atg.adapter.gsa.GSAView.executeUncachedQuery(GSAView.java:291)
at atg.adapter.gsa.GSAView.executeQuery(GSAView.java:1027)
at atg.repository.rql.RqlStatement.executeQuery(RqlStatement.java:208)
For the longest time I could not figure out what this meant and then it dawned on me as I laid in bed.
ATG expects certain Java class types for certain repository types which correspond to certain class types. I talk about this in my Date and Timestamp Repository Data Types post.
I was trying to do this RQL query:
lastModified > ?0
with this parameter:
java.util.Calendar param = java.util.Calendar.getInstance();
When I changed the parameter to type:
java.sql.Timestamp
I no longer saw this exception which makes sense because the lastModified property is of data-type timestamp.
Log4j levels
by Frank Kim on May.27, 2008, under Logging
I always forget which level is which in Log4j so I copied this from the JavaDoc for the Level class.
static Level |
ALLThe ALL has the lowest possible rank and is intended to turn on all logging. |
static Level |
DEBUGThe DEBUG Level designates fine-grained informational events that are most useful to debug an application. |
static Level |
ERRORThe ERROR level designates error events that might still allow the application to continue running. |
static Level |
FATALThe FATAL level designates very severe error events that will presumably lead the application to abort. |
static Level |
INFOThe INFO level designates informational messages that highlight the progress of the application at coarse-grained level. |
static Level |
OFFThe OFF has the highest possible rank and is intended to turn off logging. |
static Level |
TRACEThe TRACE Level designates finer-grained informational events than the DEBUG |
static Level |
WARNThe WARN level designates potentially harmful situations. |
JSTL for current URI
by Frank Kim on May.27, 2008, under JSTL
I was wondering how do get the current URI using JSTL and found this forum thread, JSTL EL for current page URL?.
If you want the actual URI of the JSP that is being rendered:
<c:out value="${pageContext.request.requestURI}"/>
If you want the original URI, which is probably the case, then:
<c:out value="${requestScope['javax.servlet.forward.request_uri']}"/>
In DSP if you want the original URI you can use the OriginatingRequest bean which implements the HttpServletRequest.
<dsp:valueof bean="/OriginatingRequest.requestURI" />
Monit and Mongrel
by Frank Kim on May.27, 2008, under Ruby on Rails
This post about Monit and Mongrel is based on this fantastic post, Monit makes Mongrel play nice!
Install Monit from source:
$ wget http://www.tildeslash.com/monit/dist/monit-4.10.1.tar.gz $ tar xvfz monit-4.10.1.tar.gz $ cd monit-4.10.1 $ ./configure -prefix=/usr (the default prefix is /usr/local) $ make $ make install
Configure Monit:
In the monit source you will find a sample monitrc which is completely commented out. You can then copy it to wherever you like and then edit it. Here is my monitrc.
# Start monit in the background (run as a daemon) and check services at
# 1-minute intervals.
set daemon 60
# Set logging.
set logfile /tmp/monit.log
# Set the list of mail servers for alert delivery.
set mailserver localhost
# Set alert recipients.
set alert foo@example.com # receive all alerts
# Start Monit's embedded web server.
set httpd port 2812 and
allow admin:monit # require user 'admin' with password 'monit'
# Mongrel - Development
# Check that Mongrel is running and that it responds to HTTP
# requests. Check its resource usage such as cpu and memory. If the
# process is not running, monit will restart it by default. In case
# the service was restarted very often and the problem remains, it is
# possible to disable monitoring using the TIMEOUT statement.
check process mongrel-dev with pidfile /rails/log/mongrel.pid
start program = "/usr/bin/mongrel_rails start -d -c /rails
-p 3000 -P /rails/log/mongrel.pid"
stop program = "/usr/bin/mongrel_rails stop
-P /rails/log/mongrel.pid"
if cpu > 50% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 60.0 MB for 5 cycles then restart
if loadavg(5min) greater than 10 for 8 cycles then restart
if failed port 3000 protocol http
with timeout 15 seconds
for 2 cycles
then restart
if 3 restarts within 5 cycles then timeout
group mongrel-dev
After you are done configuring monitrc copy it to /etc.
$ sudo cp monitrc /etc
To test the configuration:
$ sudo monit -t
Run Monit:
To start monit:
$ sudo monit
To restart monit:
$ sudo monit reload
To stop monit:
$ sudo monit quit
To see monit’s log (assuming you are using my configuration):
$ tail -f /tmp/monit.log
Learn More:
Google Doctype
by Frank Kim on May.16, 2008, under Web
Chris Weekly says Google Doctype looks like “a new, excellent resource for keeping up with webdev tips and tricks, browser compatibility issues, etc.”
Documenting the Open Web
Google Doctype is an open encyclopedia and reference library. Written by web developers, for web developers. It includes articles on web security, JavaScript DOM manipulation, CSS tips and tricks, and more. The reference section includes a growing library of test cases for checking cross-browser and cross-platform compatibility.
Google Doctype is 100% open.
- Open source
- Open content
- Open to contributions from anyone
Size of collection in a JSP/DSP page
by Frank Kim on May.15, 2008, under JSTL, Page Development, Struts
Sometimes in a JSP/DSP page you will want to get the size of a collection and unless you are within a Range, ForEach or similar droplet you won’t have access to this value.
Struts has a nice solution using the " href="http://struts.apache.org/1.3.8/struts-taglib/tagreference.html#size">" href="http://struts.apache.org/1.3.8/struts-taglib/tagreference.html#size">" href="http://struts.apache.org/1.3.8/struts-taglib/tagreference.html#size"><bean:size> tag. JSTL 1.1 has a nice solution using the fn:length function.
Here is an example of how to use Struts, DSPEL and JSTL to get the size of a collection.
<dspel:getvalueof param="book.pages" var="pages"
vartype="java.util.Collection"/>
<bean:size id="numPages" name="pages"/>
Number of Pages: <c:out value="${numPages}"/>
Number of Pages: <dspel:valueof value="${numPages}"/>
Here is an example of how to use JSTL 1.1 and DSPEL to get the size of a collection.
<dspel:getvalueof param="book.pages" var="pages"
vartype="java.util.Collection"/>
Number of Pages: <c:out value="${fn:length(pages)}"/>
Accessing RepositoryItems with JSTL
by Frank Kim on May.09, 2008, under JSTL, Page Development, Repository
Often we are accessing repository items in a JSP page like this.
<dspel:droplet name="RQLQueryForEach" var="query">
<dspel:param name="repository" bean="/betweengo/repository/Repository"/>
<dspel:param name="itemDescriptor" value="Account"/>
<dspel:param name="queryRQL" value="ALL"/>
<dspel:setvalue param="account" paramvalue="element"/>
<dspel:oparam name="output">
<dspel:valueof param="account.name"/><br/>
</dspel:oparam>
</dspel:droplet>
With JSTL we could try to display the name like this.
<c:out value="${query.account.name}"/>
If you are using JSP 2.0 you can display it even more simply.
${query.account.name}
However since account is a RepositoryItem object and there is no get method for the name property (i.e. it’s not a JavaBean object) the above will fail and produce an exception.
To get around this you can extend ATG’s RQLQueryForEach class by overriding the protected setElementParameter. In addition to setting the element parameter in the request you can set a new parameter which we will call “item”. This item is of class atg.beans.DynamicBeanMap and wraps a RepositoryItem.
public class RQLQueryForEachEL extends RQLQueryForEach {
protected void setElementParameter(DynamoHttpServletRequest pRequest,
String pElementName, Object pValue) {
super.setElementParameter(pRequest, pElementName, pValue);
DynamicBeanMap itemBean = new DynamicBeanMap(pValue, true);
pRequest.setParameter("item", itemBean);
}
}
Now we can access the repository item with JSTL like this.
${query.item.name}
Maven Assembly Convert Shell Scripts to use UNIX LF’s
by Frank Kim on May.09, 2008, under Cygwin, Eclipse, Maven, Perforce
In a previous post, Shell Scripts with Windows LF’s Fail in Latest Cygwin, I wrote about how to use Perforce to check out all files in UNIX format. However if you use Eclipse then this solution will not work because Eclipse always inserts Windows LF’s in any line you insert into a file leading to a mess with files that have both UNIX and Windows LF’s.
However if you happen to use Maven to generate / copy your shell scripts to their target directories you can take advantage of the lineEnding property in the Assembly Director. If you specify the lineEnding property to be “unix” then the outputted shell scripts will be in UNIX format. For example:
<file> <source>src/main/scripts/foo.sh</source> <outputDirectory>bin</outputDirectory> <lineEnding>unix</lineEnding> </file>
Apache not starting because of Skype
by Frank Kim on May.06, 2008, under HTTP Server
When I started Apache I saw these errors.
$ (OS 10048)Only one usage of each socket address (protocol/network address/port) is normally permitted. : make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down Unable to open logs
This was puzzling to me since Apache was starting fine before. Fortunately on this forum thread, Failure Message starting Apache, one of the respondents had diagnosed the problem.
I’ve had the same problem. I noticed that when I reboot my computer (running XP pro), sometimes Apache worked. Then, I change the port to 3128 and it worked. But, then I was illuminated by God, and do a netstat -ao (-o displays the PID) and discover that Skype was listening in port 80 instead of apache -that is, I have both set up to run at startup, thus, if apache started first, it will have port 80 available, and Skype not, and vice-versa-. So my solution was to remove Skype from startup -I don’t use it too often- and make apache (and myself) happy by making port 80 available.
All I had to do was stop Skype and everything was fine. Too bad TcpView was not able to tell me that Skype was using port 80.
