John Coleman

Ranch Hand
+ Follow
since Jul 24, 2001
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
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) {
target.setField(clazz.cast(field));
}
11 years ago
Hi,

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

target.setField(source.getField(field));

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.

TIA
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?

TIA
John
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, {http://www.entity.uk/service}CitizensAccountIntf
Duplicate Binding: SubmitMessageSoapHttp, {http://www.entity.uk/service}SubmitMessageSoapHttp
Duplicate SLT: CitizensAccountIntfLinkType, http://www.entity.uk/service
copying Sub-Assembly: D:\SOASamples\LocalCASRelayCompositeApp\build\LocalCASRelay.jar
***Warning: 1 or more consumers with 2 ports [{http://www.entity.uk/service}CitizensAccountIntf]
Port[0]: : CASRelay, {http://www.entity.uk/service}SubmitMessageSoapHttp
Port[1]: : CASEndpointBean, {http://www.entity.uk/service}SubmitMessageSoapHttp
Deleting: D:\SOASamples\LocalCASRelayCompositeApp\build\BCDeployment.jar
Created dir: D:\SOASamples\LocalCASRelayCompositeApp\dist
Building jar: D:\SOASamples\LocalCASRelayCompositeApp\dist\LocalCASRelayCompositeApp.zip
run-jbi-deploy:
Error parsing XML: java.net.ConnectException: Connection timed out: connect
[deploy-service-assembly]
Deploying a service assembly...
host=localhost
port=4848
file=D:\SOASamples\LocalCASRelayCompositeApp/dist/LocalCASRelayCompositeApp.zip
[start-service-assembly]
Starting a service assembly...
host=localhost
port=4848
name=LocalCASRelayCompositeApp
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.

TIA
John


<process
name="LCASRelay"
targetNamespace="http://enterprise.netbeans.org/bpel/LCASRelay"
xmlns="http://schemas.xmlsoap.org/ws/2004/03/business-process/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:bpws="http://schemas.xmlsoap.org/ws/2004/03/business-process/"
xmlns:wsdlNS="http://enterprise.netbeans.org/bpel/LCASRelay" xmlns:ns1="http://service.entity.uk/" xmlns:ns2="http://www.entity.uk/service">
<import namespace="http://www.entity.uk/service" location="CASRelay.wsdl" importType="http://schemas.xmlsoap.org/wsdl/"/>
<import namespace="http://www.entity.uk/service" location="Partners/CASEndpointBean/CASEndpointBean.wsdl" importType="http://schemas.xmlsoap.org/wsdl/"/>
<partnerLinks>
<partnerLink name="CASEndpoint" partnerLinkType="ns2:CitizensAccountIntfLinkType" partnerRole="CitizensAccountIntfRole"/>
<partnerLink name="LCASRelay" partnerLinkType="ns2:CitizensAccountIntfLinkType" myRole="CitizensAccountIntfRole"/>
</partnerLinks>
<variables>
<variable name="SendCitizenAccountMessageOutRelay" messageType="ns2:CitizenAccountMessageSoapOut"/>
<variable name="SendCitizenAccountMessageInRelay" messageType="ns2:CitizenAccountMessageSoapIn"/>
<variable name="SendCitizenAccountMessageIn" messageType="ns2:CitizenAccountMessageSoapIn"/>
</variables>
<sequence>
<receive name="LCASReceive" createInstance="yes" partnerLink="LCASRelay" operation="sendCitizenAccountMessage" portType="ns2:CitizensAccountIntf" variable="SendCitizenAccountMessageIn"/>
<assign name="Assign">
<copy>
<from variable="SendCitizenAccountMessageIn"/>
<to variable="SendCitizenAccountMessageIn"/>
</copy>
</assign>
<invoke name="InvokeCAS" partnerLink="CASEndpoint" operation="sendCitizenAccountMessage" portType="ns2:CitizensAccountIntf" inputVariable="SendCitizenAccountMessageInRelay" outputVariable="SendCitizenAccountMessageOutRelay"/>
<reply name="CASReply" partnerLink="LCASRelay" operation="sendCitizenAccountMessage" portType="ns2:CitizensAccountIntf"/>
</sequence>

</process>
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?

TIA
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?

TIA
John
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;
shell.setLayout(gridLayout);
ToolBar toolbar = new ToolBar(shell, SWT.NONE);
ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
itemBack.setText("Back");
ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
itemForward.setText("Forward");
ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
itemStop.setText("Stop");
ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
itemRefresh.setText("Refresh");
ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
itemGo.setText("Go");

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

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

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

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

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

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

/* event handling */
Listener listener = new Listener() {
public void handleEvent(Event event) {
ToolItem item = (ToolItem) event.widget;
String string = item.getText();
if (string.equals("Back")) {
browser.back();
}
else if (string.equals("Forward")) {
browser.forward();
}
else if (string.equals("Stop")) {
browser.stop();
}
else if (string.equals("Refresh")) {
browser.refresh();
}
else if (string.equals("Go")) {
browser.setUrl(location.getText());
}
}
}
;
browser.addProgressListener(new ProgressListener() {
public void changed(ProgressEvent event) {
if (event.total == 0) {
return;
}
int ratio = event.current * 100 / event.total;
progressBar.setSelection(ratio);
}

public void completed(ProgressEvent event) {
progressBar.setSelection(0);
}
});
browser.addStatusTextListener(new StatusTextListener() {
public void changed(StatusTextEvent event) {
status.setText(event.text);
}
});
browser.addLocationListener(new LocationListener() {
public void changed(LocationEvent event) {
if (event.top) {
location.setText(event.location);
}
}

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) {
browser.setUrl(location.getText());
}
});

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

shell.open();
browser.setUrl(newsURL);

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

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?

TIA
John
12 years ago
Hi,

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;
} POSITIONREC, *LPPOSITIONREC;
;

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?

John
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?