java

Object locking in Java

I have a class which contains a static collection that can be used across all threads. It acts as a cache. When it is first loaded from the database, it is supposed to never change. However, in rare cases, we have to clear this cache. The current way to do this is to restart the server. I want to implement a clear cache method. What is t...

How to implement generic type-safe deep cloning in a Java class hierarchy?

I have a base class, say Base which specifies the abstract method deepCopy, and a myriad of subclasses, say A, B, C, ... Z. How can I define deepCopy so that its signature is public X deepCopy() for each class X? Right, now, I have: abstract class Base { public abstract Base deepCopy(); } Unfortunately, that means that if if I have...

Is ther a way to retrieve the autoincrement ID from a prepared statement

Is there a way to retrieve the auto generated key from a DB query when using a java query with prepared statements. For example, I know AutoGeneratedKeys can work as follows. stmt = conn.createStatement(); stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); if(returnLastInsertId) { ResultSet rs = stmt.getGeneratedKeys(); ...

Swing: Floating panel next to the selected item in a JComboBox

I've created an app with a small window (the size of a combo box). I need to create a floating panel that sits outside the window, next to the selected item in a JComboBox. (See attached image). I've been reading about the JComboBox.setRenderer(customRenderer) etc. But was just wondering before I go down this path, whether it is at...

How can I read input from console till a character is typed

I want to read input from a console for example Enter input: aabbbab execute the method when pressed Enter and keep asking for input and execute till a character is typed in. At the moment I can read an input and enter it execute it.Then I need to run a program again to enter a new input. ...

Dumping "real-world" scenarios out for unit testing

I'm currently debugging some fairly complex persistence code, and trying to increase test coverage whilst I'm at it. Some of the bugs I'm finding against the production code require large, and very specific object graphs to reproduce. While technically I could sit and write out buckets of instantiation code in my tests to reproduce the...

What's the equivalent of Java's enum in C#?

What's the equivalent of Java's enum in C#? ...

Oracle Jinitiator connect mode

Hi, I am currently tracking connection issues with jinitiator but have limited experience with this technology. I want the applet to connect via a proxy, all is good at first. Console Output: Oracle Jinitiator: Version 1.3.1.13 User has overriden browser's proxy settings Proxy Configuration: Manual Configuration Proxy 192.168.1.230:3...

Table not created by Hibernate

I annotated a bunch of POJO's so JPA can use them to create tables in Hibernate. It appears that all of the tables are created except one very central table called "Revision". The Revision class has an @Entity(name="RevisionT") annotation so it will be renamed to RevisionT so there is not a conflict with any reserved words in MySQL (th...

Reading/writing binary structures: how to simplify this code?

Hi folks, I'm writing a network app, which sends and receives a lot of different kinds of binary packets, and I'm trying to make adding new kinds of packets to my app as easy as possible. For now, I created a Packet class, and I create subclasses of it for each different kind of packet. However, it isn't as clean as it seems; I've ended...

Java library to "spell out" numbers in localized fashion

I'm looking for a java library that would be able to format numbers as words for specified locale, (e.g. 17 = seventeen, 1023 = one thousand twenty three for en_US). I need this to work with monetary amounts, so decimal fractions need to be supported. The only thing I found on Google was ICU project, particularly this class. It looks so...

Google App Engine / JDO : store computed values

Since I cannot do JOIN queries against the App Engine Datastore, I would like to denormalize my entity objects a little to include computed values, in effect creating something like a function-based index. @Persistent Employee manager; @Persistent // de-normalized stored join value Integer managerDepartmentCode; This works fine,...

What is the best way to store an AES encryption key?

We use Tomcat for our java web application. There is a properties file under WEB-INF folder. AES encryption will be used to generate key and encrypt password. The encrypted password will be stored in the properties file. Where should be the encryption key stored? Is it a good idea to put the key and the encrypted password in the same p...

RSA code in both Javascript and Java

I want to find the RSA code in both Javascript and Java code. And they can return the same result in encrypt and decrypt. My purpose is: I can encrypt a message in the user's browser using Javascript (with the public key). After I can decrypt that message in my server (with private key). I found on internet but Javascript and Java retu...

java - why does graphics.drawString() not redraw?

Hi all, this is beyond me (be forgiving, it's late). I'm subclassing a WindowsSliderUI, because I want it to draw a bigger thumb (is that the right word?)(that's working) and also display the value of the slider just above it (like, for example, gtk look and feel does)(that's broken). I'm overriding the paint() method, and at the moment...

Fastest way to copy text from a File to a HttpServletResponse

Hi, I need a very fast way to copy text from a file to the body of a HttpServletResponse. Actually I'm copying byte by byte in a loop, from a bufferedReader to the response.getWriter() but I believe there must be a faster and more straightforward way of doing it. Thanks! ...

Why am I getting a crash when exiting my Activity?

I am experiencing a crash in my app when I quit (via the back button) out of my Activity. So far as I can tell this is happening in the Android codebase and not mine, but I'm not completely convinced of that. Here's the stacktrace from adb: AndroidRuntime E Uncaught handler: thread main exiting due to uncaught exception AndroidRuntim...

How to use external log4j.properties for multiple projects?

Hi, I have a main project, which depends on multiple projects (in eclipse). At the end of the project, I will generate a runnable jar and a log4j.properties. This properties file is an external file, so my client can modify it at will (email address etc). runnable.jar + log4j.properties. At the same time, those projects which the mai...

Java For-Each Loop : Sort order

Does a java for-each loop guarantee that the elements will be presented in order if invoked on a list? In my tests it does seem to, but I can't seem to find this explicitly mentioned in any documentation List<Integer> myList;// [1,2,3,4] for (Integer i : myList) { System.out.println(i.intValue()); } #output 1,2,3,4 ...

Use cases for Java concurrent utilities

I have read Java Concurrency in Practice and this is a great reference, but I would like to see a concise single page summary of the use cases of the java.util.concurrent package. For instance: Why use a concurrent collection over a synchronized collection? When should the atomic classes be preferred over explicit locking? When should...