Tuesday, 27 December 2011
My thoughts on Steve Jobs, and life in general
What is more important is that, thru reading the book, I reminded myself that most of the time we, living in a reality full of faults and problems, so much so that we tend to forget, that God has given us unlimited powers, to achieve whatever possible or impossible, limited only by our dreams and believes. It doesn't take an angel, or luck to succeed. It is our visions, our will, our persistence in wanting to succeed, that can bring us to achieve and be labelled a 'success' in the society.
We have but only one life, ignoring the fact that we can be reborn, which is something that is not a totally proven fact, and not something everyone on earth believes in. And if we do not take this one opportunity to 'make a dent to the universe', it is not just a loss to ourself. It is a loss to mankind in general as well. We who are at least given a chance to try to prove what we can be. Many others do not have the opportunity ... those sperms who have lost their race to the ovum, aborted babies, those who passed away very young for various reasons. That we, given the opportunity, can consider that as a great gift and achievement in itself.
At this point of time, we can be lost, we can be useless, or even a failure, but that does not mean we do not have a chance to prove to the world and touch the live of others, for, many successful person endure more defeat and made even bigger mistakes compared to us. The Oracle of Omaha, Warren Buffett did not earn his name being right all the time. He earned it, as he rightfully say so, because he made a few right decisions that made him way ahead of the pack.
In the end, mistakes can be forgiven, wrong decisions can be corrected and wounds can be healed. But if we give up, lose faith and are afraid to make decisions, that will be the biggest mistake of all. For, the fear in us should not be the fear that the sun is not rising tomorrow, or we might not live another day, or we might fail. The real fear is that we ourselves have lost hope and closed the door that leads to the true feelings in our heart, so much so that, the world might have lost a potential hero, without anybody knowing it. Let's not give up and continue to do what we believe in, as success can be just around the corner, much more closer than we think it is. We get out of life what we put into it.
Sunday, 16 October 2011
My thoughts on ORM
However, the buzz of ORM (Object-Relational Mapping) made me explore a few options such as JDO and JPA (I was using datanucleus implementation at one time as that is what Google AppEngine uses). At first I was quite frustrated due to the difficulties I faced to setup correctly in the initial stages. But after a while, I got more comfortable.
Once, I remember developing a Data Synchronization System early Year 2010, which is a JSP + JDO solution that synchronizes data between two databases based on preset rules (defined in JSON, which is in my universe, the king of complex data structures). I had to port this system to MySQL from Microsoft SQL Server, and my limited knowledge of MySQL really got me worried about how difficult this would be. However, it turns out to be just a snap. Generate the DDL (Data Definition Language), to create the database schema, and that's it. I might face a problem or two, which were so minor I cannot remember them, but all I know is, it is magical. The masking of Database tables as Classes and SQL in some different query language, it makes things so transparent. Not only did I not notice any performance impact, but not needing to maintain separate SQLs or DDLs means I do not have to worry about more modifications in the future if I am to add support for, say PostgreSQL. Amazing!
Fast forward to today, as I am playing with Django Framework for Python, I found that ORM are not only abstractions for database independence, but they actually mean you can play around with code more, to put the validation logic into the classes, as well as modifying them to your heart's content, which feels so much more natural than changing the database design, as well as changing the SQL separately in a program source code. Below is the source code for a simple model, enjoy!
class ServerActionHandler(basemodel.CreateUpdateInfo):
nameRegEx = re.compile(r'[a-zA-Z][a-zA-Z0-9\.]+',re.DOTALL)
name = models.CharField(max_length=128,unique=True)
state = models.CharField(max_length=1,choices=basemodel.DEFAULT_STATE_CHOICES)
method = models.CharField(max_length=256)
def clean(self):
if not self.__class__.nameRegEx.match(self.name):
raise ValidationError('ServerAction name must start with an alphabet and can only contain alphanumeric and dot (.).')
def __str__(self):
return self.name
Friday, 12 August 2011
SubEthaSMTP: Store email as files before sending out
I have written a simple handler for SubEthaSMTP that will save emails to specific folders before sending them out. First the main function to setup stuff (SMTPServer.java)
You can download it from here. (Note: There are three source class in there, with their Class Name properly highlighted).
What it does is to start an SMTP server (standard SMTP Port) in the workstation/server you are running it on.
You will need to configure SMTPMessageHandlerFactory to set the appropriate smtp host, smtp port, smtp user and smtp password to send the email to.
Whenever a mail is received, it will write the contents of the mail in folder: outbox/{fromemailaddr}/{date}/{time}
It will work for normal mail (HTML/plain) and mail with attachments, but not message/rfc822 type.
Also, because I am not able to read BCC headers from the mail received via SMTP, instead of processing the TO, CC, and BCC in the mail content, I will send out the mail to everyone separately, putting them in the TO field for each email sent.
Please help to test out and comment accordingly. Thanks.
Friday, 5 August 2011
Apache Camel and SubEthaSMTP
- perform filtering on the sender and recipient to weed out inactive or barred users
- log email messages to files or database
- instead of sending mail, lookup recipient preference and optionally send via other channels such as IM (Instant Messaging) or SMS.
- Configure multi-channel send, e.g. Send email to recipient(s), and send notification to them via IM or SMS to inform them about the email header and sender, e.g. You have a mail from development.rants@gmail.com - "Automated Backup for Server BBIGGG completed successfully"
- Analytics or Statistics to determine how many emails are sent to a specific recipient, and how many emails with a particular subject is sent.
1: SMTPServer server = new SMTPServer(new MyMessageHandlerFactory()); 2: server.setMaxConnections(100); 3: AuthenticationHandlerFactory fact = new LoginAuthenticationHandlerFactory( new Validator() ); 4: server.setAuthenticationHandlerFactory(fact); 5: server.start();
Sunday, 24 July 2011
Apache Camel: Email notification when files are placed in FTP folder
For this article, you can use the Apache Camel project created from my previous blog article. Also, let's enable Apache Camel logging so we can see debug output for ease of understanding what's going on. Verify if you already have log4j.properties in your project (should be in Other Sources -> src/main/resources -> <default package> folder under your project. If it is there, you might want to skip to the FTP and Email section.
To do so, first, ensure that you add dependency for slf4j-log4j12 for the appropriate version of slf4j you have (mine is 1.6.1). You can follow the instructions in my previous article to add the dependency (if you are like me, using NetBeans 7 and Maven).
Next you need to click on the Files tab in your NetBeans project, go to your project and expand to src\main, right-click and select New -> Folder. Create a folder named 'resources' (without quotes, of course).
After that, right-click on the resources folder you have just created, and select New -> Properties File. When the New Properties File dialog appear, enter log4j as the filename. The Folder should be src\main\resources (if you indeed right-clicked on the resources folder). As per screenshot below:
Enter following info into your log4j.properties file:
log4j.rootLogger=TRACE, out log4j.logger.org.apache.camel=DEBUG log4j.appender.out=org.apache.log4j.ConsoleAppender log4j.appender.out.layout=org.apache.log4j.PatternLayout log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%nThe file should be accessible from Projects tab in the following navigation under your Project folder: Other Sources -> src/main/resources -> <default package>
Next, add dependencies for both camel-mail and camel-ftp.When done, amend your routing code to:
context.addRoutes(new RouteBuilder() {
public void configure() {
from("ftp://ftp.myftp.com/test?username=myuser&password=mypass&noop=true")
.setBody().simple("Hi, new file uploaded to ftp. Name: ${header.CamelFileName}")
.setHeader("subject").simple("Received filename: ${header.CamelFileName}")
.to("smtps://smtp.gmail.com?username=development.rants@gmail.com&password=******");
}
});
The above example (fictitious account info), will check the test folder in ftp.myftp.com for user: myuser. When found, it will send an email to my gmail account development.rants@gmail.com. The sender is of the same email account. An email will be sent for every file found in the ftp account, so for testing purposes, please ensure that you do not have too many files in this folder.
You may also want to modify the call to Thread.sleep in App.java to a longer time, e.g. 60 seconds, as below:
Thread.sleep(60000);
Next, build and run the project, and voila! You will start receiving emails when files are uploaded into the ftp folder being watched.
For this article, notice that I am using noop=true parameter for the ftp url. That will means the file will be left in that folder as it is. This is useful if you are running the program a few times and wants to be able to test without needing to re-upload files to the ftp folder.
For more options you may want to refer to the Camel FTP component documentation here.
Enjoy!
Wednesday, 6 July 2011
Using NetBeans 7.0 to create an Apache Camel project without Spring dependency
3) Expand your Project from the Project Tab, right-click on Dependencies folder and select Add Dependency...
4) In the Add Dependency dialog box, enter camel-core in the Query textbox under the Search tab. Once the Search Results appear, expand org.apache.camel: camel-core folder and click on the version you require, or the latest version available (currently latest is version 2.8.0) [bundle]. Central or Local should not matter, as you should only have Local if you have downloaded it before (either directly in your project or indirectly in other projects with camel-core dependency).
Click on the Add button.
5) Now expand the Source Packages in your project and you should be able to find App.java. Modify the main class to the code below:
public static void main( String[] args ) throws Exception
{
CamelContext context = new DefaultCamelContext();
// add our route to the CamelContext
context.addRoutes(new RouteBuilder() {
public void configure() {
from("file:src/data?noop=true").
choice().
when(xpath("/person/city = 'London'")).to("file:target/messages/uk").
otherwise().to("file:target/messages/others");
}
});
// start the route and let it do its work
System.out.println("Starting");
context.start();
Thread.sleep(20000);
System.out.println("Done");
// stop the CamelContext
context.stop();
}
You will need to add the necessary dependencies.
6) You can now run the application, which will read any files in your src/data folder, and parse the xml, and move the file to either the target/messages/uk or target/messages/others folder depending on whether the person -> city node contains the value 'London' or not.
If you followed my previous blog entry 'Using NetBeans 7.0 to create a new project using Apache Camel', you should have sample message1.xml and message2.xml under your src/data folder, which you can use for testing purposes.
All folders here are relative to your project folder. Until my next blog, Good Luck and Good Bye!
Friday, 1 July 2011
Using NetBeans 7.0 to create a new Apache Camel project
3. Click on Next, and in the Maven Archetype wizard. Under the Archetypes from remote Maven Repositories, search for camel-archetype-java (The version I am using is 2.7.2. You might be using a never version), and click on the next button. If it doesn't work for you, follow step 4. Otherwise, skip to step 5.
Artifact Id: camel-archetype-java
Apache Camel
Apache Camel is a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration.As I do a lot of coding in Java, and my company is mainly developing a PHP-based Content Management System, I am very keen to look for ways to provide a seamless integration and simplify PHP development by allowing most scheduled and batch jobs to be run via Java.
Using Apache Camel, I can easily fetch a file from a folder, and send it via email to a specific destination. It is not to say that PHP cannot do this, but using Java, I can easily allow 10 PHP web-applications to share the same Java engine. The advantage of this is, besides email, sending of SMS, XMPP message can be transparent to PHP as well, thus less config needed, as most of the one-time config can be centralized in the Java backend.
Apache Camel also allows you to easily communicate between servers by leveraging on JMS or sockets, so if you have hundreds of PHP applications strewn across 10 servers, you can have a simple Apache Camel app installed in all 10 servers that will just watch for existence of a file and dump it to a JMS queue, available for pickup by another set of servers that will then send out the Emails or SMS.
Sounds interesting? Continue following my blog as I will add new articles that slowly introduce a few great ways on how Apache Camel can be used.