|
|
this blog has moved and is now hosted at blogger. please update your bookmarks and feeds. the latest post can be found here: make.money.fast
Fri, Feb. 24th, 2006, 01:17 am fully organised
this blog has moved and is now hosted at blogger. please update your bookmarks and feeds. the latest post can be found here: fully organised
this blog has moved and is now hosted at blogger. please update your bookmarks and feeds. the latest post can be found here: development environment (two)
i've decided to change my weblog provider. the blog is now hosted by Blogger, a Google subsidiary, instead of here at livejournal. Blogger has a bit more of a 'serious' reputation than LJ (it doesn't keep asking about how i'm feeling, like an insecure teenage girl in the middle of a relationship crisis, for instance!) the customisation options also seem a lot better, and it has full API access via an Atom XML-RPC / HTTP interface. one thing that was a bit annoying was porting my old posts across. i ended up exporting from LJ as an Atom feed and then manually importing each post by editing the HTTPS requests to the API in Blogger. i tried writing a short bit of Java to accomplish this, but kept getting 500 server errors? the export/import process also lost all the comments from LJ, but there aren't that many. another thing that interests me is the AdWords integration, which i'd like to play with (i have some clients that require the same service) and the ability to get listed on search engines and blog listings sites automatically, to attract more readers. the templates for page design are also much more flexible - the CSS / HTML is fully editable so i'm going to be adjusting the layout for a while, and see what looks best. and now... please update your bookmarks (to http://grkvlt.blogspot.com/) for more of the same tasty chunks of random goodness:
this post is about some stuff i discovered is possible, using only standard 100% java, to add functionality to interfaces. i'm assuming you all have a good grounding in OOP and understand inheritance and interfaces and so on. in particular, the java object model is a tree, starting with java.lang.Object and descending down by way of child classes that extend this base or root class. these sub-classes can only extend one parent class, however they can implement several interfaces. an interface is a statement of an API contract that must be fulfilled by the implementing object. it usually consists of a number of public methods that will be written by the developer as part of the new object. for example, the java.lang.Runnable interface (for thread creation) requires that the object have a public void run() method. this means that any object where (o instanceof Runnable) is true will be able to have o.run() called on it. similar to interfaces are abstract classes, which are like ordinary objects, but with some methods declared using the abstract keyword. these methods must then be implemented by any child classes that extend it. since java only allows single-inheritance, interfaces are used to allow a class to extend some parent class and still conform to multiple other method signatures or contracts. however, if you have developed an interface that must be implemented by a large number of your classes, and these classes must also extend some different parent to inherit required functionality, it can be a pain to have to write the same implementation of an interface method many times. what you would like to do is have interfaces that don't just specify method signatures, but actually contain the code for an implementation of those methods that can then be called on any implementing class. sadly this is not possible - an interface cannot contain any Java code. all is not lost, though, since it is possible to define a nested inner class as part of your interface. an inner class is just like a standard class definition, except it is nested in the code for another class. inner interfaces are also possible, but not discussed here. what i discovered is that it is possible to write a number of method implementations, fields, static initialisers and so on in this inner class, and when compiled, any object that implements the interface will also have access to them, and present them to other classes depending on visibility. here is an example... suppose we have an interface Kitten which defines methods public String getText() and public void doSomething(). the code looks like this:
public interface Kitten {
public String getText();
public void doSomething();
}
but, we also want all Kitten objects to have access to some utility methods public void doMore(String s) and public String findOut(). These could be added to the interface signature, and implemented by any class that implements Kitten, but instead we will add an inner class called Helpful and a reference to it in the outer interface, as follows:
public interface Kitten {
public String getText();
public void doSomething();
public Helpful h = new Helpful();
public class Helpful {
private String THING = "java interfaces";
public void doMore(String s) {
System.out.println("doing more with " + s);
}
public String findOut() {
return THING;
}
}
}
so, this interface now has the inner class added. in order to see how it works, we will need to write a test program that implements the interface and then try accessing some of the inner class methods. the implementation is simple, just write the body for the two methods declared by Kitten, for instance:
public class Potato implements Kitten {
// we could also add "extends Whatever" if nescessary
public String getText() {
return "this is some text!";
}
public void doSomething() {
for (int i = 0; i < 3; i++) System.out.println("stage " + i);
}
}
with this class we ought to be able to call both the Kitten and the Helpful methods, so we should write a test class to demonstrate:
public class Test {
public static void main(String[] argv) {
Potato p = new Potato();
System.out.println(p.getText()); // from Kitten
p.doSomething(); // from Kitten
p.h.doMore("string"); // from Helpful
System.out.println(p.h.findOut()); // from Helpful
}
}
and when compiled, the output from the Test class is as follows:
$ javac Test.java
$ java Test
this is some text!
stage 0
stage 1
stage 2
doing more with string
java interfaces
this is pretty much as expected. the inner class methods are called on the Helpful instance h that is part of the interface specification. they could also be called directly. for example, if there was a static method in Helpful named doThis(), it could be called from anywhere inside a class implementing Kitten as Helpful.doThis() or from anywhere else in your code as simply Kitten.Helpful.doThis(). i think this is probably an abuse of the java object system, and certainly not "best practice" for OOP in any way, but it may be helpful for some situations where you want to avoid repeated boilerplate code and need the benefits of inheritance for utility methods but your objects must descend from a library class, and interfaces are the only extension point available. one thing to bear in mind, however, is that the nested inner class methods are treated by the compiler as static and there may be scope issues with access to the parent class. this could be worked around by passing a reference to this into some of the methods, or similar tactics. happy coding!
i've been quite busy lately developing a web application for a friend's small business. in doing so, i've discovered (re-discovered) a good set of tools, and i think i now have the best dev-env that i can manage for free. so, this blog is a discussion and description of what my setup is like and what tools i'm using and why... i use Apple Macintosh computers almost exclusively now (a G4 15" powerbook, from my friend gav, who died last year and left it to me. thanks. you're still remembered...) and the only thing that bugs me is the single-button mouse, and the fact that one has to control-click to access context menus etc. anyway, i recently invested in a microsoft notebook mouse, a small two button plus scroll-wheel optical USB mouse (about 33% the size of a standard mouse). this simply works just like you'd hope it would, ie. the right mouse button is the equivalent of control-clicking, and pops up the context menu for the item you click on, in the finder, java applications, eclipse, whatever. the scroll-wheel also works exactly like windows, and both of these features have raised my productivity no end. the trackpad on a laptop is inherently tricky as it is, and i'm glad to be rid of it. in terms of software, i had always used vi on Unix systems, which i learned in university, and graduated on to vim eventually, since it's also available for XP. however I recently tried the Sun NetBeans 4.1 IDE and really liked it. i then got Eclipse 3.1 (originally from IBM) for OS X and installed it to try with my current project, since there seemed to be more plug-ins available for it. i really like both of them, having used Microsoft Visual Studio on XP, and find they are quite similar. (as an aside, although VS2005 is quite expensive, and the free .NET command-line tools provided with the SDK are OK, Microsoft have now made available Express Edition of VS for C#, VB and even C++ for free, as in beer, for at least the next year.) anyway, of the two open source IDEs, i think i prefer the way Eclipse works, with its views and perspectives and enormous collection of editor and environment plug-ins. in my development setup, i use Eclipse with the standard set of web and enterprise plug-ins. these are all packaged together as the Web Tools Platform or WTP. this gives me XML, HTML, CSS, JSP, and many more editors and views. because of this, i can now edit all the files in my web application with an Eclipse native editor and integrated debugging, error reporting/detection and validation (except for JSPs, since the editor doesn't handle taglibs that are imported with a <%@ taglib ... %>directive in a sub-fragment included in the main page using a <%@ include file= ... %>directive. this is perfectly legal in the JSP spec, since the include directive is processed before compilation (like #include pre-processor directives in C), unlike the <jsp:include ... >tag and similar, which are processed at runtime. this means that my struts JSPs which use the html, bean and logic taglibs all show up with warnings, since the editor can't see the included taglib declaration. all quite annoying, and i had to turn off JSP validation, although that still doesn't always work. i tried a bunch of other plug-ins, but the better ones were all 30-day trials of commercial software (MyEclipse, EnterpriseWorkbence, ExpressWorkshop etc.) or poorly integrated and incomplete. the WST project is the official Eclipse set of J2EE/Web editing tools, originally the IBM Web Tools for Eclipse product, and is currently at version 1.5 and works fine for my purposes. other Eclipse features i like are the ant integration and incremental compilation. i use Apache ant as my build mechanism because it allows me to do command-line builds and also deployment and other tasks in a repeatable way. i have tasks set up for compilation, war creation, database creation, code generation, revision control, backups and app-server deployment. Eclipse has an ant view that shows all available targets and an editor for your build.xml file that interprets file inclusions and external task definitions correctly, as well as parsing the EL syntax in properties and auto-completion of tags as you write. the auto-complete works in a similar way to Microsoft VS IntelliSense and gives you a pop-up list of possible methods, fields and so on whenever appropriate, as well as adding closing braces, parentheses and so on. good if you're forgetful, since you have less of the oh-damn-i-forgot-a-close-curly-brace-there-that's-why-it-won't-compile moments, after ten minutes of staring at the code... the next plug-in i use, and the main reason for switching from NetBeans to Eclipse, is subclipse, which is a subversion revision control tool. it integrates into the package/file navigator view, showing me which of my project files is under revision control (currently the src/, conf/ and web/ directories) and whether they are out of sync with the repository. i can also do dioffs against the repository, tag and branch revisions easily and commit changes, all using the Team context menu. (see above re: the new mouse... ;) Subversion is a great RCS, but it took me a while to decide to use it. the first version control system i ever used was sccs, and i then moved to RCS because it was free. CVS is the next obvious step, it being a descendant of RCS, but i never got around to using it. i was looking for something to use with my current project, and had heard lots of good things about subversion, but the lack of integration with my IDE stopped me trying until my decision to switch. now, however, i'm finding it very simple and easy to use, and the setup for OS X was very simple. i have the server running from xinetd, rather than WEBDAV, with svn://localhost/ URLs for the repositories. i also use a tool called SvnX to browse svn repositories and manipulate the with an OS X GUI. i also tried using a Finder plug-in that gives a subversion context menu for each file under revision control, and integrates subversion with the Finder icons, but so far i've not found it that useful. i manipulate the working copy of my project just like i would normal files, and only commit and update using the UI inside Eclipse, since that's where the editing is going on. also, the command-line client gives me a much more powerful set of tools when i really need them. ( update the finder integration plugin is called SCPlugin) one thing i haven't been using the Eclipse UI for is managing the application server or the database. i tend to view these components as infrastructure rather than part of my development environment, and usually control them using their native interfaces. i do have ant targets that allow me to deploy database schemas and upload web-app war files, but that's only as part of the deployment/build cycle. the app-server i'm using is Jakarta Tomcat 5.5.12. this version is usually built with the J2SE 5.0 runtime, but the version of OS X i have (10.3.9) doesn't have that available (although 10.4 does) so i'm stuck running 1.4.2 instead. fortunately, there's a compatibility package available that drops in on top of the Tomcat install to allow it to run on 1.4.2 Java environments. interestingly, the latest Tomcats all use the Eclipse Java incremental compiler, so only need a runtime not a full JDK deployment, although you can use javac or jikes if you want. to manage the Tomcat instance i just use the provided scripts, although i had to provide a SystemStarter directory and an option in the /etc/hostconfig file to get it to run on startup. i just copied the Apache scripts written by Apple. the database i use is MySQL 5.0 with the InnoDB tables, since they provide better transaction and relational support. my persistence engine manages most of the actual data and schema definitions and updates (more later ...) but i also downloaded MySQL Administrator from the MySQL site to configure and monitor the database server itself. they provide a .dmg disk image for the OS X version of the 5.0 server, which installs in /usr/local/mysql and also adds a preference pane for automatic startup options. the actual OS X installer is a .pkg file and the whole experience is painless. i find a lot of companies don't give much thought to the OS X versions of their products, thinking "it's just BSD, isn't it?" so this is a welcome change. well, that's all for today. tomorrow i'll talk a bit about the libraries and other packages i'm using to actually write the application...
so, palm have released some new handhelds (the T|X which is a black, wifi enabled full-featured top of the range model, and the Z22, an ipod white style entry lrvrl unit. i actually own the Zire 72s, which my family bought me for my thirtieth birthday (best. present. ever.) anyway, while reading about these, i noticed a comment in a review talking about pocketmod. this is a paper based organiser, which is easily made by printing the design on a sheet of A4 and folding/cutting as indicated. the site allows a whole range of pages to be chosen for your organiser, and they will form a nice, compact 8-page booklet. for some reason this origami organiser appeals to me, maybe just the alliteration? i actually use the moleskine notebooks, which are excellent, with cool features like a set of perforated pages that cab be torn out easily, and a little pocket at the back, as well as a ribbon bookmark and an elastic band to keep the notebook shut. i just happen to like writing with a pen and paper, i guess, and it lasts longer than a laptop, since the batteries never run down... as soon as a lightweight laptop with the perfect handwriting interface comes out (newton, where are you now?) i'd buy that, until then, compromise.
right. you would suppose, in this country, that the money you deposit into your bank account is yours, for you to withdraw and spend as you please, whenever you want? certainly, if you went into a branch of your bank, you would want to have the ability to withdraw cash for whatever purpose you saw fit? well, let me tell you about what happened today, in the high street kensington branch of the royal bank of scotland... i asked to withdraw a (largish) sum of money from my account, over GBP 1000.00 basically. naturally, the cashier wanted identification. i produced my bank card, and my current, valid, british passport. these were accepted, along with a cheque, written out right there, from a complete cheque book, for the amount, payable to 'CASH' and signed, again in front of the cashier. there followed a lengthy (~10 minutes) phone conversation, which i couldn't hear, presumably with my branch. this resulted in 4 (four) pages of faxed copies of a photocopy of my passport and bank card and signature, made at my branch in edinburgh when the account was opened (i was actually a member of staff at the time, so identity checks were even more rigorous). once my identity had been confirmed here in london satisfactorily, the cashier began to fill in a 'large withdrawal pro-forma' document. this went along fine, with me supplying my full name and the amount of my money i wished to withdraw, until i was asked 'can you tell me why you want this cash?' this struck me as an unnecessary and pointless question, so i answered 'no!' apparently, this is beyond the scope of a 'large withdrawl pro-forma' and appeared to be unacceptable. i offered other suggestions, such as 'customer did not say' and 'none' and similar, however the cashier insisted that these could not be entered into the (paper) form. being a curious sort, i asked why, and was told the magic words 'fraud' and 'security' . i can't even begin to imagine what kind of fraud is prevented by asking an (authenticated) customer why they want to withdraw their money. anyway, an impasse appeared to have been reached here, and the only solution proffered was for the chief cashier to be summoned. i was warned that this may take some time (i had been trying to complete the transaction for 15-20 minutes by then, and the cashier was obviously in an office about ten meters away) however i graciously agreed to wait, and sat down to read the copy of terror inc - tracing the money behind global terrorism that i had just bought. (sadly, nobody noticed. i don't know why i bother, sometimes...) the result of all this is that i was unable to withdraw my money from my bank account in a branch of my bank. i believe i may have said some inappropriate words to the cashier, chief cashier and other assorted bank staff who had gathered to watch the proceedings. my incredulity at being unable to complete what i assumed was an everyday banking requirement seemed to baffle and confuse them. i even ventured outside to the atm and withdrew a (smaller than i needed) sum to show that i was indeed allowed to obtain and use my own money. since it was too late for me to do anything useful, even if they had relented and done the right thing, i simply shouted and stormed out. after a calming cigarette or four, i decided to phone the head office. there i spoke to a nice young lady who gave the same reason - it is part of anti money-laundering safeguards put in place to stop fraud and terrorism and the like. however, once i pointed out that, supposing i was minded to take my money and do something illegal with it, i didn't think that the prospect of having to lie and tell a cashier that i needed the money for a second-hand car purchase, rather than say, buying raw opium from afghanistan which i intended to exchange for plastic explosives in libya, would really bother me. strangely, she immediately accepted this, as did her supervisor, and they apologised, admitting that it seemed like the wrong kind of security question to have asked. i still don't have the money i need, though, so i'm going to have to try again tomorrow. this time, though, i will go there a lot earlier, just in case... is it just me, or are these pointless invasions of privacy really, really, annoying? i can see absolutely no basis in law for the question, and as i pointed out, a criminal will just make something up! so there is no security gained or fraud prevented. actually, it's not just me - see bruce schneier's weblog, passim, for plenty of similar examples, and weep.
cool. the guardian reader's editor, ian mayes got back to me. he says that: there does seem to be a conflict [between the desire to eradicate bad science reporting and the health/wellbeing article contents] and he will pass my email to the editor of the [weekend] magazine, and also to the editor of the guardian. so there. writing to newspapers can be fun and useful. so hopefully if enough people decide its a good thing, there will be better researched science and medicine articles and less junk and 'alternative' therapies being promoted by journalists who should know better... for those who haven't heard of him, ian mayes is the 'reader's editor' of the guardian, in charge of the daily corrections and clarifications column, also a weekly column discussing some of the content and contextual issues of that week's reporting. he's also put together a collection of these columns in a couple of books, which i highly recommend. the guardian was the first paper to have an editorial position like this, and i think it reflects well in their content - they listen to their readers a lot more than any other paper i know of.
today i wrote a letter to the guardian - a first for me. my paper experience is usually the standard one-way variety. however, the thing that moved me to put pen to gmail account was the bad science column by ben goldacre. i really enjoy his column, which exposes a lot of the misguided or sometimes just plain fraudulent writing that goes on in journalism, press releases and marketing/advertising. there was a good explanation of his motives and experiences printed when the guardian moved to it's new berliner format. anyway, he is currently bashing the BBC for a wrong-headed online article about the therapeutic effects of spiraling water implosion electric fields something-or-other alternative rubbish. now, today on pages 78-79 of the weekend magazine, there is a list of ways that you can limit the damage done by cigarettes. one of these is 'too many computers' - apparently those pesky PCs leak EMF radiation, which could KILL you, and your CHILDREN if you're not careful. but, help is at hand. unplugging your computer, rather than just switching it off will (somehow?) make a difference. quite what that would be, i don't know, since a switched off computer uses no power, and unplugging will just mean it uses the same amount of no power. i don't have the complicated medical and scientific background that the author of the holistic therapy file has (available for GBP 14.99 the article helpfully informs me!) i suppose, so who am i to say that the 'evidence' that 'certain plants can help soak up EMFs' is faulty? it just disappoints me that after all the good work that 'bad science' has done, and the excellent reputation the guardian has for reporting, and good science journalism, they missed this. i don't know whether doctor goldacre is responsible for overall editing or vetting of science content, but if he isn't someone else should be. i think it's all part of our society's tendency to think that scientific ignorance is nothing to be ashamed of, but rather something to be proud of. i remember a short article in the paper lamenting the fact that astronomical objects such as planets were defined by 'arrogant scientists' - the nerve! people really need to understand the world around them, and how it works, and science is the tool that we use for that. but these days, it's far trendier to claim an interest in 'alternative' therapies and solutions, as some kind of childish rebellion against authority. the problem is, the authorities are right - that's the definition of the word - scientists are authoritative on matters of, well, science! you can contact him at bad.science@guardian.co.uk with stories of bad science reporting and other misinformation, or go to his website at http://www.badscience.net/. update: i got a reply from dr goldacre (speedy, only an hour after emailing him) he says - "what can i do, they wont print a word i say about them" which i'm not sure what to make of? does he mean that he's not allowed to bad-mouth the paper that feeds him, and must leave it's hand unbitten? update ii: he now suggests i take it up with the reader's editor, which i have done. more to follow...
|