John Coleman

Ranch Hand
+ Follow
since Jul 24, 2001
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Coleman

Yes I settled for using ConcurrentHashMap and obtaining an iterator over the keys and then using get to obtain the latest value.

In our application reading is much lower in volume compared to writing to the map, and I think I noted that this map is really designed to work the other way around. I'm not sure whether this matters though.
We have a scenario where we want to decouple an application from a service works over the network.

In this application a number of threads produce prices, and a single thread for the network must transmit the prices as they arrive.

However, as we are consuming the incoming prices, if a new price for a specific item is received before the last one is transmitted we just want to send the new one.

We solve this by putting prices in a map keyed by their ID, and then iterating over the entry set. However, the drawbacks are that we have to copy the map in a synchronized block that excludes the new prices coming in for a time to achieve the copy, and then clear the input map.

Then the process the entry set of the working copy map.

Is there a way to get the best of both worlds i.e. not have the issues surrounding copying maps and using iterators, or the problem of using a queue which just sends things in order, even if an item is effectively to be replaced?

Originally posted by Ilja Preuss:
Even with Java 1.0, you don't need to use instanceof in this case - just cast to the type that is required.

If you can't change the code of the methods, generics or covariant return types can't help you, or so it seems to me.

Unfortunately I think your comment above is right, and I have to code:

if (field instanceof StringField) {
target.setField((StringField) field);
} else
if (field instanceof DoubleField) {
target.setField((DoubleField) field);
} else
if (field instanceof IntField) {
target.setField((IntField) field);
} else
if (field instanceof CharField) {
target.setField((CharField) field);
} else
if (field instanceof BooleanField) {
target.setField((BooleanField) field);
} else
if (field instanceof UtcDateOnlyField) {
target.setField((UtcDateOnlyField) field);
} else
if (field instanceof UtcTimeOnlyField) {
target.setField((UtcTimeOnlyField) field);
} else
if (field instanceof UtcTimeStampField) {
target.setField((UtcTimeStampField) field);

But that's inefficient to execute and maintain.

Something like this looks good, but since there is no setField<T>, not possible.

setField(field.getClass(), field, target);

static <T> void setField(Class<T> clazz, Field field, FieldMap target) {
11 years ago

Can I use generics or covariant return types to somehow avoid using iinstanceof here:


In this case both the setter and getter demand field to be a subclass of type Field, rather than of Field as presently.

These are types and methods from a third party library, I cannot change the implementations.

11 years ago
I have a load of Java Clip objects and I want to play phrases by playing one clip after another. How do I do this? As it is the system just plays each clip as it is queued.

Is there any decent book or site about Java sound?
12 years ago
I want display individual characters centralised inside a box so that I can create a sort of grid with them equally spaced - this would be like a centered baseline monospaced characterset.

I can't see how with lower case letters once can discover how much to shift them to get them central, although this can be achieved somewhat with caps.

Is there any way to make my own font up perhaps, and bundle that with the application?

12 years ago
I have an unusual web service that I've implemented using BPEL. All it is required to do is accept the message on an inbound port and hand the same message over to a remote web service port, then relay the reply back.

The problem is that since both partner links use the same WSDL (accept the port address of course), there is duplication of some element, like the operations for example. When I build a composite application using the process, it will not build or deploy:

Duplicate PT: CitizensAccountIntf, {}CitizensAccountIntf
Duplicate Binding: SubmitMessageSoapHttp, {}SubmitMessageSoapHttp
Duplicate SLT: CitizensAccountIntfLinkType,
copying Sub-Assembly: D:\SOASamples\LocalCASRelayCompositeApp\build\LocalCASRelay.jar
***Warning: 1 or more consumers with 2 ports [{}CitizensAccountIntf]
Port[0]: : CASRelay, {}SubmitMessageSoapHttp
Port[1]: : CASEndpointBean, {}SubmitMessageSoapHttp
Deleting: D:\SOASamples\LocalCASRelayCompositeApp\build\BCDeployment.jar
Created dir: D:\SOASamples\LocalCASRelayCompositeApp\dist
Building jar: D:\SOASamples\LocalCASRelayCompositeApp\dist\
Error parsing XML: Connection timed out: connect
Deploying a service assembly...
Starting a service assembly...
Failed execution of Start
ERROR: (JBIMA1202) Lifecycle command startServiceAssembly failed for LocalCASRelayCompositeApp :
* Component: com.sun.bpelse-1.0-2
ERROR: (BPELSE) Service unit failed to load deployment Error while loading BPEL file C:\Sun\SDK\domains\domain1\jbi\system\deployment\LocalCASRelayCompositeApp\com.sun.bpelse-1.0-2_LocalCASRelayCompositeApp-LocalCASRelay\LCASRelay.bpel, Exception details are Cannot parse BPEL
D:\SOASamples\LocalCASRelayCompositeApp\nbproject\build-impl.xml:214: Service assembly deployment failed.
BUILD FAILED (total time: 31 seconds)

Is there a neat way around this? Please show by example code if you can.


xmlns:wsdlNS="" xmlns:ns1="" xmlns:ns2="">
<import namespace="" location="CASRelay.wsdl" importType=""/>
<import namespace="" location="Partners/CASEndpointBean/CASEndpointBean.wsdl" importType=""/>
<partnerLink name="CASEndpoint" partnerLinkType="ns2:CitizensAccountIntfLinkType" partnerRole="CitizensAccountIntfRole"/>
<partnerLink name="LCASRelay" partnerLinkType="ns2:CitizensAccountIntfLinkType" myRole="CitizensAccountIntfRole"/>
<variable name="SendCitizenAccountMessageOutRelay" messageType="ns2:CitizenAccountMessageSoapOut"/>
<variable name="SendCitizenAccountMessageInRelay" messageType="ns2:CitizenAccountMessageSoapIn"/>
<variable name="SendCitizenAccountMessageIn" messageType="ns2:CitizenAccountMessageSoapIn"/>
<receive name="LCASReceive" createInstance="yes" partnerLink="LCASRelay" operation="sendCitizenAccountMessage" portType="ns2:CitizensAccountIntf" variable="SendCitizenAccountMessageIn"/>
<assign name="Assign">
<from variable="SendCitizenAccountMessageIn"/>
<to variable="SendCitizenAccountMessageIn"/>
<invoke name="InvokeCAS" partnerLink="CASEndpoint" operation="sendCitizenAccountMessage" portType="ns2:CitizensAccountIntf" inputVariable="SendCitizenAccountMessageInRelay" outputVariable="SendCitizenAccountMessageOutRelay"/>
<reply name="CASReply" partnerLink="LCASRelay" operation="sendCitizenAccountMessage" portType="ns2:CitizensAccountIntf"/>

12 years ago
I'm having fun with the new system tray in Java 1.6, but how can I get the popup menu font to match the font used by the other popup menu icons in the tray?

12 years ago
I'm using a JTextPane (plain/text) as a scrollable message board in a chatroom application.

Sometimes if a line wraps, the component does not show the bottom line as if it calculates the actual size of the control wrong. If you resize the container then the control gets bigger and displays the last line, along with the vertical scroll bar getting longer. You can then pull the scroll bar down to see the last line.

Is there a way to get the JTextPane component to always have the right size after text is added to its docment?

12 years ago
This is the code using the asyncExec technique. It crashes the JVM.

import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class SWTBrowser {

private final Display display = new Display();
private final Shell shell = new Shell(display);
private final Browser browser = new Browser(shell, SWT.NONE);

private String aNewsURL;

public SWTBrowser(String newsURL) {
this.aNewsURL = newsURL;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
ToolBar toolbar = new ToolBar(shell, SWT.NONE);
ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);

GridData data = new GridData();
data.horizontalSpan = 3;

Label labelAddress = new Label(shell, SWT.NONE);

final Text location = new Text(shell, SWT.BORDER);
data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.horizontalSpan = 2;
data.grabExcessHorizontalSpace = true;

data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.verticalAlignment = GridData.FILL;
data.horizontalSpan = 3;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;

final Label status = new Label(shell, SWT.NONE);
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;

final ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
data = new GridData();
data.horizontalAlignment = GridData.END;

/* event handling */
Listener listener = new Listener() {
public void handleEvent(Event event) {
ToolItem item = (ToolItem) event.widget;
String string = item.getText();
if (string.equals("Back")) {
else if (string.equals("Forward")) {
else if (string.equals("Stop")) {
else if (string.equals("Refresh")) {
else if (string.equals("Go")) {
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
if ( == 0) {
int ratio = event.current * 100 /;

public void completed(ProgressEvent event) {
browser.addStatusTextListener(new StatusTextListener() {
public void changed(StatusTextEvent event) {
browser.addLocationListener(new LocationListener() {
public void changed(LocationEvent event) {
if ( {

public void changing(LocationEvent event) {
itemBack.addListener(SWT.Selection, listener);
itemForward.addListener(SWT.Selection, listener);
itemStop.addListener(SWT.Selection, listener);
itemRefresh.addListener(SWT.Selection, listener);
itemGo.addListener(SWT.Selection, listener);
location.addListener(SWT.DefaultSelection, new Listener() {
public void handleEvent(Event e) {

display.asyncExec(new Runnable() {
public void run() {
if (!browser.getUrl().equals(aNewsURL)) {

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {

public void setURL(String newsURL) {
aNewsURL = newsURL;

12 years ago
I have a Swing application that I need to pop up an SWT browser, and then be able to change the URL of the browser depending on settings in Swing.

All examples of the SWT browser seem to be a simple single thread, completely pointless examples.

If I try and create more than one SWT display, I get the JVM crashing. Similarly I can't create an SWT browser and then invoke the setURL on it from another thread.

It seems unstable and unfriendly the way it has been implemented.

Is there a way just to create the browser, hide it when it's "closed" and then pop it up again with a new URL (from another thread) as required?

12 years ago

I want to call a function in C++ that returns a pointer to a structure array, like this

BOOL __stdcall GetPositionBlotter(int* iCount, void* RecArray);

Where RecArray is elements like this

typedef struct _POSITIONREC {
char SymIdent[20];
char SymExchange[15];
char SymSecType[20];
char SymExpiry[30];
char SymRight[30];
char SymCurrency[20];
DWORD Quantity;
double MarketPrice;
double MarketValue;
double AverageCost;
double UnrealizedPNL;
double RealizedPNL;

How do I get that C++ structure back to Java? I suppose I need an array of a class? I guess somehow I need to instantiate those classes in C++ and load the fields in a loop and pass them back as the result.

Can anyone provide simple example code of how I do that please?

13 years ago
It turns out to be a silly mistake on creating objects to pass into the dispatcher. It only manifested under the faster conditions available without using synchronized.
I just tried something different. I put a counter in the queued objects so I can see which instance is being processed, and the output reads correctly, with each object processed in sequence exactly as it should be and behaving as per the usual models. So somehow it seems my toString I use to output the objects data before is misleading?

I suspect that without using the synchronize, somehow the delivery of the sample packets to the audio system is adversely affected?
If the bug is in my code, how come using synchronize to wrap my processing results in a correct function? Furthermore, how is it possible at all that the collection can ever return the same refence using a remove method?