Tuesday, August 25, 2009

Turn Java Security Manager Off In Google App Engine SDK

For some reasons you might want to turn Java Security Management off for Google Appengine Development Environment.

I need this for properly handling of Tapestry5 error page. With Java SecurityManager enabled it produced the following errors:


Could not initialize class org.apache.tapestry5.corelib.components.Loop

java.lang.NoClassDefFoundError: Could not initialize class
org.apache.tapestry5.corelib.components.Loop

java.lang.NoClassDefFoundError:
org/apache/tapestry5/corelib/components/Loop$1


To turn it off, you should replace two classes in SDK, that are responsible to installing Security Manager.

  1. Close all Eclipse instances

  2. Locate appengine-tools-api.jar: %YOUR_ECLIPSE_FOLDER%\plugins\com.google.appengine.eclipse.sdkbundle_1.2.2.v200907291526\appengine-java-sdk-1.2.2\lib\appengine-tools-api.jar

  3. Replace original classes com\google\appengine\tools\development\DevAppServerFactory$CustomSecurityManager.class and com\google\apphosting\utils\security\SecurityManagerInstaller.class with ones provided below (you may want to do a backup first)



Thats all. Now after starting App Engine project in eclipse you should see the following output in console:


Skip install SecurityManager
Create dummy CustomSecurityManager
The server is running at http://localhost:8080/


Just keep in mind that original App Engine cloud still have those Security Managers installed.

FYI here is the sources of those classes:

DevAppServerFactory.java
package com.google.appengine.tools.development;

import java.security.Permission;

public class DevAppServerFactory {

public static class CustomSecurityManager extends SecurityManager {

@Override
public void checkPermission(Permission perm) {
// Do nothing
}

public CustomSecurityManager(DevAppServer devAppServer) {
System.out.println("Create dummy CustomSecurityManager");
}

}

}


SecurityManagerInstaller.java
package com.google.apphosting.utils.security;

import java.net.URL;

public class SecurityManagerInstaller {

public static void install(URL... urls) {
System.out.println("Skip install SecurityManager");
}

}


Download Google App Engine/Java 1.2.2 Security Manager patch here (3 KB)

Friday, August 07, 2009

.Net Collections Lookup Performance



using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

namespace TechNoir.NorCal.BL.Tests
{
[TestFixture]
public class PerformanceTests
{
[Test]
public void CompareLookupSpeed()
{
bool warmUp = true;

for (int j = 1; j <= 10; j++)
{
int n = j * 1000;

Console.WriteLine("\nTest {0}; n = {1}\n", j, n);

List<int> list;
Dictionary<int, object> dictionary;
List<int> sortedList;
IQueryable<int> queryable;

PrepareTestData(n,
out list, out dictionary, out sortedList, out queryable);


DateTime start = DateTime.Now;
foreach (int item in list)
{
Assert.That(dictionary.ContainsKey(item));
}
DateTime end = DateTime.Now;

Console.WriteLine("IDictionary.ContainsKey : "
+ (end - start).TotalMilliseconds);


start = DateTime.Now;
foreach (int item in list)
{
Assert.That(sortedList.BinarySearch(item) != -1);
}
end = DateTime.Now;

Console.WriteLine("List.BinarySearch : "
+ (end - start).TotalMilliseconds);


start = DateTime.Now;
foreach (var item in list)
{
Assert.That(list.Contains(item));
}
end = DateTime.Now;

Console.WriteLine("List.Contains : "
+ (end - start).TotalMilliseconds);


start = DateTime.Now;
if (j <= 3)
{
foreach (int item in list)
{
int localItem = item;
Assert.That(queryable.Any(i => i == localItem));
}
}
end = DateTime.Now;

Console.WriteLine("IQueryable.Any : "
+ (end - start).TotalMilliseconds);


if (warmUp)
{
warmUp = false;
j--;
}

}
}

private static void PrepareTestData(
int n,
out List<int> list,
out Dictionary<int, object> dictionary,
out List<int> sortedList,
out IQueryable<int> queryable)
{
list = new List<int>();

var rand = new Random(42);

for (int i = 0; i < n; i++)
{
int x;
do
{
x = rand.Next(int.MaxValue);
} while (list.Contains(x));

list.Add(x);
}

queryable = list.AsQueryable();

dictionary = new Dictionary<int, object>();

foreach (var item in list)
{
dictionary.Add(item, null);
}


sortedList = new List<int>();
sortedList.AddRange(list);

sortedList.Sort();
}
}
}

Monday, August 03, 2009