Showing posts with label open source software. Show all posts
Showing posts with label open source software. Show all posts

Tuesday, July 10, 2012

GIMP 2.8 has arrived

GIMP (GNU IMAGE) 2.8 is here.

It just occurred to me the other day that GIMP was working on 2.8. I thought I would like to check it out.  When I got there (www.gimp.org), it is there.

I like the single view mode (under menu Windows, Single-Window-Mode).  It solved the only complaint I had with GIMP in the past. That is to click twice in order to re-focus on the other window when switching between tool/editor/layer windows.

I have not checked out what else is new in it. GIMP is one of a few open source projects I appreciated and used frequently. I would expect to continue creating a lot of digital assets from GIMP for my iPhone app projects.

Friday, March 30, 2012

Combine gmail and Java mail library to create your email compaign

If you use gmail for your company, you can run your own email campaign without paying those email campaign companies.  Write a small script or Java program to pull user data from your database and create a pretty HTML based email template. If you want to track them, insert the tracking in the HTML content.  Use the following code to start sending emails to your users.  Just be sure not to abuse the gmail account.


import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class Mailman extends javax.mail.Authenticator
{
    private String smtpServer;
    private String from;
    private String password;
    private boolean useAuth;


    public Mailman(String sender)
    {
        from = sender;
        smtpServer = "smtp.gmail.com";
    }


    public String getSmtpServer() { return smtpServer; }
    public void setSmtpServer(String svr) { smtpServer = svr; }


    public String getSender() { return from; }
    public void setSender(String fm) { from = fm; }


    public String getPassword() { return password; }
    public void setPassword(String passwd) {
        password = passwd;
        if (passwd != null && passwd.length() > 0) {
            useAuth = true;
        } else {
            useAuth = false;
        }
    }


    /*
     * This method sends plain text mail.
     */
    public void send(String subject, String body, String to, String cc)
    {
        try
        {
            Message msg = createMessage(subject, to, cc);
            msg.setText(body);
            Transport.send(msg);
        }
        catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }


    /*
     * this method sends mail with file attachments
     */
    public void send(String subject, String body, List<String> attachPaths, String to, String cc)
    {
        try
        {
            Message msg = createMessage(subject, to, cc);
            Multipart multipart = new MimeMultipart();
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(body);
            multipart.addBodyPart(messageBodyPart);


            for (String fileAttachment : attachPaths) {
                MimeBodyPart attachment = new MimeBodyPart();
                DataSource source = new FileDataSource(fileAttachment);
                attachment.setDataHandler(new DataHandler(source));
                attachment.setFileName(new File(fileAttachment).getName());
                multipart.addBodyPart(attachment);
            }
            msg.setContent(multipart);


            Transport.send(msg);
        }
        catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }


    protected Message createMessage(String subject, String to, String cc) throws Exception
    {
        Properties props = System.getProperties();
        props.put("mail.smtp.host", smtpServer);
        Session session = null;
        if ( useAuth ) {
            props.put("mail.smtp.starttls.enable","true");
            props.put("mail.smtp.auth", "true"); 
            // SSL
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");


            session = Session.getInstance(props, this);
        } else {
            session = Session.getInstance(props);
        }
        // create a new message
        Message msg = new MimeMessage(session);


        if (from == null) {
          from = System.getProperty("user.name");
        }
        msg.setFrom(new InternetAddress(from));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        if (cc != null) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
        }


        // set the subjecttext
        msg.setSubject(subject);
        // set some other header information
        msg.setHeader("X-Mailer", "s3ar-JavaMail");
        msg.setSentDate(new Date());


        return msg;
    }


    @Override
    public PasswordAuthentication getPasswordAuthentication()
    {
        // you can read the password from a file
        // which should set mode as 0400


        return new PasswordAuthentication(from, password);
    }
}

Tuesday, August 9, 2011

Programmically create a Mule 3.x Inbound point to a Mule service

I use JMS inbound endpoint as an example.  Different types of inbound endpoints differ slightly.

1. Create an inbound point configurer of the target inbound type

public class JmsInboundConfigurer implements MuleContextAware
{
    ServiceCompositeMessageSource msgSource;
   
    public void setMuleContext(MuleContext context) {...}
    public MuleContext getMuleContext() {...}
    public void setService(Service svc) {...}
    public Service getService() {...}
    public void setConnector(Connector conner) {...}
    public Connector getConnector() {...}
}

In the mule-config.xml, add

<spring:bean id="my.configurer" class="com.acme.JmsInboundConfigurer" init-method="init">
    <spring:property name="service">
        <spring:ref bean="my.service"/>
    </spring:property>
    <spring:property name="connector">
        <spring:ref bean="activemq.connector"/>
    </spring:property> 
</spring:bean>

2. Statically configure an inbound point and read it back to match the configuration

<jms:activemq-connector name="activemq.connector" 
 brokerURL="tcp://localhost:61616"/>
<jms:endpoint name="jms.endpoint" queue="..."
 connector-ref="activemq.connector" exchange-pattern="request-response"/>

JmsInboundConfigurer.init() would make a good place to output all the information.

this.msgSource = 
(ServiceCompositeMessageSource)getService().getMessageSource();
logger.info(msgSource.toString());

Use msgSource.getEndpoint("jms.endpoint") or msgSource.getEndpoints() to read back end points to compare with the static configuration

3. Comment out the static configure and create it programmatically

The following code fragment creates and add a JMS inbound point to the service assigned

EndpointURIEndpointBuilder builder = new EndpointURIEndpointBuilder(uri, this.muleContext);
builder.setName("jms.endpoint");
builder.setConnector(this.connector);
builder.setExchangePattern("request-response");

// builder.setProperty("", xyz);  // if any
if (transaction needed) {
    MuleTransactionConfig txConf = new MuleTransactionConfig();
    txConf.setAction(ACTION_BEGIN_OR_JOIN);
    txConf.setMuleContext(muleContext);
    txConf.setTimeout(30000);
    JmsTransactionFactory factory = new JmsTransactionFactory();
    factory.setName("a-good-name");
    txConf.setFactory(factory);
    builder.setTransactionConfig(txConf);
}
builder.setInitialState(AbstractService.INITIAL_STATE_STARTED);
builder.setResponseTimeout(10000);
builder.setDeleteUnacceptedMessages(false);
builder.setDisableTransportTransformer(false);
builder.setEncoding("UTF-8");
InboundEndpoint inept = builder.buildInboundEndpoint();
muleContext.getRegistry().registerEndpoint(inept);
msgSource.addSource(inept);

The inbound endpoint should work at this point.

Wednesday, August 3, 2011

To begin this blog

I am a software engineer, and I code.

I use the Internet as a reference, language features, open-source software, best practices and quick suggestions on day-to-day issues. There are a lot of good articles, but they are easily buried under by other search engine optimized sites. I decided to log what I found and my thoughts on the fine points. This will definitely help myself, and hopefully will help other people.