ANT: Copy a directory recursively
To mimic the xcopy command in windows, use the following:
<copy todir="destinationDir"><fileset="sourceDir"></fileset></copy>
You only need to use the <include> tag if you want to specify which files you want included
Log4j, SMTPAppender, and Authentication
I was trying to hook up my log4j configuration to my gmail account so that I could be immediately alert for errors. Pretty simple use case… But I started getting errors like:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first.
and
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Authentication required
I have reason to believe SMTPAppender is borked on anything authentication related because your authentication properties are read after the javax.mail.Session is created. The following is code for an overridden SMTPAppender that takes over the session creation.
import java.security.Security;
import java.util.Properties;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import org.apache.log4j.net.SMTPAppender;
public class SMTPSSLAppender extends SMTPAppender {
public SMTPSSLAppender() {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
}
@Override
protected Session createSession() {
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.host", getSMTPHost());
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.quitwait", "false");
Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{ return new PasswordAuthentication(getSMTPUsername(),getSMTPPassword()); }
});
return session;
}
}
And the log4j.properties would have something like:
log4j.appender.mail=SMTPSSLAppender
log4j.appender.mail.SMTPHost=smtp.gmail.com
log4j.appender.mail.SMTPUsername=myusername
log4j.appender.mail.SMTPPassword=mypassword
log4j.appender.mail.BufferSize=1
log4j.appender.mail.Subject=ZOMG some error occured!
log4j.appender.mail.To=my@email.com
log4j.appender.mail.threshold=error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
Of course, overriding session creation breaks a lot of things, but it still has the 20% of functionality that 80% of folks will use.
File Containing Eclipse Application Debug VM Arguments
Normally, you can edit the VM Arguments by going to Run > Debug Configurations… > Eclipse Application > [selecting the new configuration] > Arguments. If you want to edit this file externally, the XML file containing this data is located in: [Your workspace directory]i\.metadata\.plugins\org.eclipse.debug.core\.launches\[Plugin name].launch
Prevent Directory Listings Using .htaccess
A simple fix, just add the following line of code to your .htaccess :
# Prevents directory listing Options -Indexes
Converting WordPress Collation to UTF8
For some foolish reason, the recent upgrades to WordPress 2.8 and 2.7 changes the collation of the database tables. This results in the removal of special characters like Japanese, Chinese, or Korean. All your characters would be lost if you upgraded because the collation is latin1_swedish_ci instead of utf8_general_ci. You’re lucky if you hadn’t used those characters before, but if you are planning to in the future, you can update your collation to UTF8 by running the following SQL queries in PHPMyAdmin:
ALTER TABLE wp_comments DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE wp_links DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE wp_options DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE wp_postmeta DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE wp_posts DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE wp_terms DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE wp_term_relationships DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE wp_term_taxonomy DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE wp_usermeta DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ALTER TABLE wp_users DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
Downloading and Installing wsimport
The Java Web services book I’m reading right now uses wsimport to show how this tool can generate a client with ease.
- Download wsimport at JAX-WS from java.net. There should be a download link in the menu. At the time of this article, the latest version 2.1.7 can be downloaded here.
- Download the file and put it to a directory like C:/Java and execute by double clicking on the JAR file if you are on Windows
- From the extracted files, set the CLASSPATH environmental variable in Windows to point to the bin folder
Now you can run wsimport from the command prompt and have all sorts of fun.
Deleting All Posts and Comments from a WordPress Blog
The best way to understand the tables is to look at the WordPress Databae Design page, so you are aware of the dependencies. I wanted a nearly clean copy of WordPress, so I executed the following SQL statements:
delete from wp_comments; delete from wp_links; delete from wp_posts; delete from wp_postmeta; update wp_term_taxonomy set count = 0 where 1 = 1; delete from wp_term_relationships;
This deletes all comments, links, posts, post metadata, resets the count for the tags/categories/links to 0, and deletes the relationships. I had to delete more than comments and posts, otherwise the tables would be in an inconsistent state. For example, it may end up showing more than 1 post under a certain tag when all posts have been removed. Thus, that explains why I had to set count to 0 for wp_term_taxonomy and remove all the links.
Updating All Internal Links in a WordPress Blog
Suppose you are switching domains and you want all links to point to your new blog. There are 3 places where you need modifications: the author comment URL, the comment content, and the post content. Comments are important as people may refer to your blog in the comments section, so it needs to be updated.
Perform the following queries:
UPDATE wp_comments SET comment_author_url = replace(comment_author_url, 'old URL','new URL') UPDATE wp_comments SET comment_content = replace(comment_content, 'old URL','new URL'); UPDATE wp_posts SET post_content = replace(post_content, 'old URL', 'new URL');
Reference: How to Move WordPress Blog to New Domain or Location
Importing Large WordPress WXR Files
I had to help someone import a 15MB WordPress WXR file today which failed because it was much too big (getting Fatal Errors). I ended up splitting the WRX as described by the article Import Large WordPress WXR File.
Each WRX needs 3 parts t o comply with the schema:
Header:
<rss version=”2.0?
xmlns:content=”http://purl.org/rss/1.0/modules/content/”
xmlns:wfw=”http://wellformedweb.org/CommentAPI/”
xmlns:dc=”http://purl.org/dc/elements/1.1/”
xmlns:wp=”http://wordpress.org/export/1.0/”>
<channel>
including all info like category, tags, etc to just before the first <item>
Body:
Your <item> tags
Footer:
</channel>
</rss>
I had accidentally saved the file without UTF8 encoding and that messed up the import badly. You need to make sure it is UTF8 if you have special characters.
Modifying the WordPress Comments Template
Reference: WordPress Codex
“This tag includes the file comments.php from your current theme’s directory. If that file is not found, it will instead include wp-content/themes/default/comments.php.”
You can copy the default comments.php and add modifications and place it in your theme’s directory. It is a good template to start with.