Category Archives: Tricks

Export Eclipse Plugin, Assert Errors

Here is my scenario:
I import Eclipse RSE( Remote System Explorer) plug-ins into my workspace and I want to build and test it myself. There are no errors in the workspace and running and debugging is all OK. And I want to export these plug-ins out: Export->Plug-in Development->Deployable plug-ins and fragements -> … An error dialog tells me that “Error occurred during the operation. A zip file containing the build logs has been generated and placed at …”. And exact that *.zip file and there is a @dot.bin.log, saying something like:
4. WARNING in D:\eclipse\workspace\org.eclipse.rse.core \src\org\eclipse\rse\core\ SystemTypeMatcher.java (at line 31)
assert pattern != null;
^^^^^^
'assert' should not be used as an identifier, since it is a reserved keyword from source level 1.4 on
----------
5. ERROR in D:\eclipse\workspace\org.eclipse.rse.core \src\org\eclipse\rse\core\ SystemTypeMatcher.java (at line 31)
assert pattern != null;
^^
Syntax error on token "!=", = expected

I know this error. But I already set the Compiler Compliance Level to 1.5 already, and in the workspace there is no errors complaining about this.

Googling all over the net and there is no clear solution to solve this problem. Someone says that modifying ANT script build.xml by adding source=”1.4″ will solve this problem. But the problem is that using Export->… the build.xml will always be regenerated.

After reading the source of plug-in org.eclipse.pde.build, I found that there is an option for the script generator to specify javacSource in build.xml. You can just add two lines in build.properties file like the following:
javacSource = 1.4
javacTarget = 1.4

And exporting plug-ins won’t have errors any more.

It feels great to have sources in hand when solving problems. 😀

Posted in Bug Fix, Eclipse, Tricks | Tagged , | 3 Comments

VMWare Player, Ubuntu and Mac OSX

So you want to play some other operating systems for some new features, or test your applications on other OSs? Take a try on VMWare Player. It is free.

Along with downloading and installing VMWare, you should also download OS images ( also called as Virtual Applicances). I think you should not miss Ubuntu 8.04 Desktop image (512M). I found that Ubuntu 8.04 running quite smoothly inside VMWare on my laptop with duo-core 2.2Ghz CPU and 2G memory. And I am lazy to install GIMP on my Vista, so running GIMP for some graphic manipulations may be one of Ubuntu jobs. And developing C++ or Java applications inside Ubuntu may be another job for me. And lots of other testing jobs. So owning an Ubuntu desktop is necessary.

(But I find a big inconvenience in my Ubuntu. I can input anything but having great difficulties in inputting 5 characters: “, ‘, ~, `, ^. I need to press that key and follow a space key to input these characters. I struggled hours to fix it but failed. So if you need a solution, please be kind to inform me.)

By the way, why not taking a try on Mac OSX Leopard. Download its 3.3G (expanded to 8G) Mac OSX Leopard x86 image (You should know that it is a pirate copy), following its instructions to install and patch. As using VMWare Player, you have to modify *.vmx directly to load *.iso patch into Leopard:

ide1:0.fileName = “D:\Downloads\Maxxuss-AMDPCNET-v1.0_1043.iso”
ide1:0.deviceType = “cdrom-image”

And you may also change

guestOS = “freebsd-64”

to

guestOS = “freebsd”

So no 64-bit CPU warning in starting this x86 Mac copy.

Well, I feel little excitement to Leopard as it is too slow. So maybe it is just for feature peeking or some small tests. It is not ready for daily work. Hope you have enough patience for the 2-3 days’ image download time and 12G hard disk spaces wasting.

Posted in Tricks | Tagged , , , | 2 Comments

Port Mapping on Linux

Mapping port 80 to inner port 8080:
/sbin/iptables -t nat -A PREROUTING -i eth+ -p tcp –dport 80 -j REDIRECT –to-port 8080

Well, I use this trick to avoid firewall blocking.

For binding more HTTP servers on different server implementations:

/sbin/iptables -t nat -A PREROUTING -i eth+ -p tcp -d xxx.xxx.xxx.xxx –dport 80 -j REDIRECT –to-port 8080
8080 is another server implementation’s listening port

Posted in Tricks | 3 Comments

How To Replace “/” With “” in .bat File

Here is an example to replace string with a different delimiter in Windows’ *.bat file.

for /f "tokens=1-15 delims=/" %%i in ("%1") do (
set _X1_=%%i
set _X2_=%%j
set _X3_=%%k
set _X4_=%%l
set _X5_=%%m
set _X6_=%%n
set _X7_=%%o
set _X8_=%%p
set _X9_=%%q
set _XA_=%%r
set _XB_=%%s
set _XC_=%%t
set _XD_=%%u
set _XE_=%%v
set _XF_=%%w)
set _XPATH_=%_X1_%
if not "%_X2_%" == "" set _XPATH_=%_XPATH_%\%_X2_%
if not "%_X3_%" == "" set _XPATH_=%_XPATH_%\%_X3_%
if not "%_X4_%" == "" set _XPATH_=%_XPATH_%\%_X4_%
if not "%_X5_%" == "" set _XPATH_=%_XPATH_%\%_X5_%
if not "%_X6_%" == "" set _XPATH_=%_XPATH_%\%_X6_%
if not "%_X7_%" == "" set _XPATH_=%_XPATH_%\%_X7_%
if not "%_X8_%" == "" set _XPATH_=%_XPATH_%\%_X8_%
if not "%_X9_%" == "" set _XPATH_=%_XPATH_%\%_X9_%
if not "%_XA_%" == "" set _XPATH_=%_XPATH_%\%_XA_%
if not "%_XB_%" == "" set _XPATH_=%_XPATH_%\%_XB_%
if not "%_XC_%" == "" set _XPATH_=%_XPATH_%\%_XC_%
if not "%_XD_%" == "" set _XPATH_=%_XPATH_%\%_XD_%
if not "%_XE_%" == "" set _XPATH_=%_XPATH_%\%_XE_%
if not "%_XF_%" == "" set _XPATH_=%_XPATH_%\%_XF_%

For more information, please key in “for /?” in command line.

Posted in Tricks | 1 Comment

Failed to Download Files from FTP Servers

Today, I came across a problem: I could not download files given by “ftp://…”. I could ping that server, but could not link to it. I was using FlashGet and I tried to change some preferences but didn’t work. At last I turned off my Windows XP’s firewall and downloading worked at once. At later, I re-open the firewall and add my FlashGet or Filezilla to the trusted applications list.

Posted in Tricks | Comments Off on Failed to Download Files from FTP Servers

Get Projects List in Eclipse Workbench

http://dev.eclipse.org/newslists/news.eclipse.newcomer/msg13300.html

Add
org.eclipse.core.resources
in the Require-Bundle section in your plugin MANIFEST.MF file
and invoke code:

ResourcesPlugin.getWorkspace().getRoot().getProjects()

Posted in Eclipse, Java, Tricks | Comments Off on Get Projects List in Eclipse Workbench

Clean Eclipse Plugins’ Build Path Errors

Sometimes, Eclipse may have errors in calculating build paths, which may result in that some projects are marked as red cross error icons and further building is interrupted. The common error message for this would be “Project … is missing required project …”.

Try to check out and make sure that there are no errors. But try to clean and rebuilt the project do not clean out that error mark? Please try to close the project, and then re-open it, clean and rebuilt the project. It may help.

Posted in Eclipse, Tricks | Comments Off on Clean Eclipse Plugins’ Build Path Errors

Enablement Expression and Property Tester

Source: http://www.eclipse.org/webtools/wst/components/server/runOnServer.html

Enablement Expression Performance

Enablement expressions allow you to create arbitrarily complex expressions using ands, ors, nots, and Java code. For performance reasons, it’s always better to use the expression support instead of using Java code, which is slower or may be in plugins that aren’t loaded yet. However, simple Java tests can provide much better flexibility and do not need to cause large delays.

The best practice is to use enablement expressions as much as possible to filter down the number and types of objects that are applicable. As a final check, a property tester can then be used. However, it is still important to use expressions first, even if you know you’ll be using a property tester for the last check – this will keep the performance up for all the objects that are filtered out.

Adding a Property Tester

To add a property tester to an enablement expression, try using the following example. In your plugin.xml, add the propertyTesters extension point:

<extension point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
id="org.eclipse.myplugin.propertyTester"
namespace="org.eclipse.myplugin"
properties="someProperty"
type="java.lang.Object"
class="org.eclipse.myplugin.internal.MyPropertyTester">
</propertyTester>
</extension>

Inside your plugin, add and implement the property tester code:

package org.eclipse.myplugin.internal;
import org.eclipse.core.expressions.PropertyTester;

public class MyPropertyTester extends PropertyTester {
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
// cast receiver and/or expectedValue
// if (xx) // check some dynamic property
return true;
// else
//return false;
}
}

You can now use a property expression within the enablement:

<test property="org.eclipse.myplugin.someProperty" value="true"/>

Posted in Eclipse, Tricks | Comments Off on Enablement Expression and Property Tester

Create a Process and Read Its Output

This is a snippet of windows C++ code to create a process and read its output until some specific string is printed. This is useful for programmer to start a server and only to return until the server is started, as sometimes server may take some seconds to initialize.

BOOL CreateProcessAndWait(char* cmdline, int flag,
STARTUPINFO *si, PROCESS_INFORMATION *pi,
const char* keyString)
{
HANDLE hOutputReadTmp,hOutputRead,hOutputWrite;
HANDLE hErrorWrite;
SECURITY_ATTRIBUTES sa;

// Set up the security attributes struct.
sa.nLength= sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;

// Create the child output pipe.
if (!CreatePipe(&hOutputReadTmp, &hOutputWrite, &sa,0)) {
printf("CreatePipe\n");
}
// closes one of its std output handles.
if (!DuplicateHandle(GetCurrentProcess(),hOutputWrite,
GetCurrentProcess(),&hErrorWrite,0,
TRUE,DUPLICATE_SAME_ACCESS))
printf("DuplicateHandle");
// Create new output read handle and the input write handles.
// Set the Properties to FALSE. Otherwise, the child inherits
// the properties and, as a result, non-closeable handles to
// the pipes are created.
if (!DuplicateHandle(GetCurrentProcess(),hOutputReadTmp,
GetCurrentProcess(),
&hOutputRead, // Address of new handle.
0,FALSE, // Make it uninheritable.
DUPLICATE_SAME_ACCESS))
printf("DupliateHandle");

ZeroMemory(si, sizeof(STARTUPINFO));
si-&gt;cb = sizeof(STARTUPINFO);
ZeroMemory(pi, sizeof(PROCESS_INFORMATION));
flag = CREATE_NEW_PROCESS_GROUP | flag;
si->dwYCountChars = 5000;
si->dwFlags |= STARTF_USECOUNTCHARS;
si->dwFlags |= STARTF_USESTDHANDLES;
si->hStdOutput = hOutputWrite;
si->hStdError = hErrorWrite;

int ret = CreateProcess(NULL,
cmdline, /* command line */
NULL, /* Security */
NULL, /* thread */
TRUE, /* inherit handles */
flag, /* start flags */
NULL, /* winenv, */
NULL, /* current directory */
si, pi);
if (ret != -1)
{
CHAR lpBuffer[256];
DWORD nBytesRead;
DWORD keyLength = strlen(keyString);
DWORD index = 0;
memset(lpBuffer, 0, sizeof(lpBuffer));

DWORD dwTimeoutThreadId;
HANDLE hTimeoutThread = CreateThread(
NULL, // no security attribute
0, // stack size, BUGBUG:Try to use the limit size!
(LPTHREAD_START_ROUTINE) TimeoutThread,
(LPVOID) pi->hProcess, // thread parameter
0, // not suspended
&dwTimeoutThreadId); // returns thread ID

if (hTimeoutThread == NULL)
{
printf("No time out thread for specific string %s.\n",
keyString);
}
else CloseHandle(hTimeoutThread);

while(TRUE)
{
if (ReadFile(hOutputRead,lpBuffer + index,
sizeof(lpBuffer) - index,
&nBytesRead,NULL) && nBytesRead)
{
if ((index + nBytesRead >= keyLength)
&& strstr(lpBuffer, keyString))
{
break;
}
if (index + nBytesRead > sizeof(lpBuffer)
- keyLength) {
// move
for (DWORD i = 0; i < keyLength; i++) {
lpBuffer[i] = lpBuffer[index + nBytesRead
- keyLength + i];
}
index = keyLength;
memset(lpBuffer + keyLength, 0,
sizeof(lpBuffer) - keyLength);
} else {
index += nBytesRead;
}
}
}
}
return ret;
}

Posted in C++, Snippet, Tricks | Comments Off on Create a Process and Read Its Output

Why JavaScript’s Date#getMonth Start From Zero?

Today, one of my colleague asked me a question about JavaScript’s document.lastModified, here was my answer for her:

javascript:dd=new Date(document.lastModified);alert(dd.getFullYear() + “.” + (dd.getMonth() + 1) + “.” + dd.getDate() + “//” + dd.getHours() + “:” + dd.getMinutes() + “:” + dd.getSeconds());

Then she asked me why #getMonth should plus one why others need not. I knew that from the ECMAScript specification, Date#getMonth start from 0 to 11, representing January to December, but I couldn’t answer her question, cause I couldn’t figure out why only #getMonth needs such a hacks which confuse a lot of developers.

After searching a lot on Internet, I got nothing and gave up to found out why. Maybe those specification designers were C or C++ developers who always thought things should start from zero.

Posted in JavaScript, Tricks | Comments Off on Why JavaScript’s Date#getMonth Start From Zero?