David C. Meyers

Greenhorn
+ Follow
since May 20, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by David C. Meyers

I'm attempting to send an email with an image. The image could be an attachment, or it could be imbedded. I don't have a preference. I am reading the image from a database and through a third party API I get a Point[]. I use the point array to create a BufferedImage. I'd like to try to attach the BufferedImage to the email. I have found sample code to attach an image from a file, but I'd rather not write my image to a file. My problem would be solved if I could create a DataSource from a Buffered image. I'd appreciate any advice.

// code to create the BufferedImage
Point[] pointArray = ...;

BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = image.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, 200, 200);

g2.setColor(Color.black);

Point lastPoint = null;
for (int i = 0; i < pointArray.length; i++)
{
if (-1 == pointArray[i].x && -1 == pointArray[i].y)
{
lastPoint = null;
continue;
}

if (null != lastPoint)
{
g2.drawLine(lastPoint.x, lastPoint.y, pointArray[i].x, pointArray[i].y);
}

lastPoint = pointArray[i];
}

g2.dispose();
// end BufferedImage Code


// code to send a message with attachment
// from http://java.sun.com/developer/onlineTraining/JavaMail/contents.html#SendingAttachments
// Create the message
Message message = new MimeMessage(session);

// Fill its headers
message.setSubject("Embedded Image");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));

// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "<H1>Hello</H1>" +
"<img src=\"cid:memememe\">";
messageBodyPart.setContent(htmlText, "text/html");

// Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart("related");
multipart.addBodyPart(messageBodyPart);

// Create part for the image
messageBodyPart = new MimeBodyPart();

// Fetch the image and associate to part
DataSource fds = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader("Content-ID","<memememe>");

// Add part to multi-part
multipart.addBodyPart(messageBodyPart);

// Associate multi-part with message
message.setContent(multipart);
18 years ago
Perfect! If only JavaRanch had a forum to solve world hunger.

Thanks
--
Dave
I'm not sure that I can provide an exact amount, but I did find pages to load quite slowly after reaching 50 rows or so of results. The project that I was working on slowly converted all of our xsl to jsp pages.

Good Luck.
--
Dave
That worked! However, the xml that is produced from this is now prepended by 'sch' for example when I call:
System.out.println("messageDocument = " + messageDocument.xmlText());

I get
<sch:message service="MathService" xmlns:sch="http://www.common.com/common/schema"><sch:request><sch:A>10</sch:A><sch:B>20</sch:B><sch peration>none</sch peration></sch:request><sch:response><sch:result/><sch:Result>200</sch:Result></sch:response></sch:message>

I can't have 'sch:' in my xml result that I return. Any suggestions on the best way to return cleaner xml?

Thanks.
I'm attempting to use xml beans to parse an xml message. My xml message is:
String xml = <message xmlns='http://www.company.com/common/schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='schema0.xsd' type='Service'>...</message>

If I use the xml above and I call parse using:
MessageDocument.Factory.parse(xml);

I get back a valid MessageDocument object. I'd like to remove the namespace attributes from the xml String and still be able to parse the string using the Factory parser.

For instance:
String xml = <message type='Service'>...</message>
XmlOptions options = new XmlOptions();
Map namespaceMap = new HashMap();
namespaceMap.put("xmlns", "http://www.company.com/common/schema");
namespaceMap.put("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
namespaceMap.put("xsi:schemaLocation", "schema0.xsd");
options = options.setLoadAdditionalNamespaces(namespaceMap);
MessageDocument messageDocument = MessageDocument.Factory.parse(xml, options);

This does not work I get:
com.bea.xml.XmlException: error: The document is not a message@http://www.company.com/common/schema: document element namespace mismatch expected "http://www.company.com/common/schema" got ""

I am unable to find any examples of setLoadAdditionalNamespaces, but it seems like this should be the way to supply namespaces other than in the xml message itself.

Any ideas?

Thanks.
--
Dave
Many purists would cringe at static methods for utils, and encourage the singleton approach. However, either approach provides the opportunity for threading issues. Thus I would recommend instantiating your util class before using it.

Also, beware of static SimpleDateFormats. It is NOT thread safe. If two calls hit the same static DateFormat unexpected results may occur. I've had this happen on a system that runs 100 tps. Two different times were being formated in the same ms. They came out the same with the hour of the first time, and the minute and second of the second time.
19 years ago
The synchronized part is in reference to iterating through the collection. If while iterating the Vector has an entry added or removed, the iterator will through a ConcurrentModificationException. You can have multiple threads call get or add on the Vector without issue as long as you are not iterating through it.

From the API:

The Iterators returned by Vector's iterator and listIterator methods are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Vector's elements method are not fail-fast.
19 years ago
May I suggest:

String uri = "/Servlet/Case.html";
int lastSlashIndex = url.lastIndexOf('/');
int lastDotIndex = url.lastIndexOf('.');
String name = uri.substring(lastSlashIndex, lastDotIndex);

Good luck.
19 years ago
I'm sure there are many ways to handle this, but here is one suggestion. In your calcStrValue method, use a boolean to keep track of if your token is even or odd. If it is odd check to see if it is a number. If it is even, and it is not +/- throw an exception of your own design and catch it from the caller then display an error message.
19 years ago
HashMap is exactly what you want to achieve you're goal. It will be fast and easy.
19 years ago
Jan,

It is a common (and good) practice to catch your Java exceptions and re-throw a specific business exception that you create. This way your client can catch this business exception and display a proper error message instead of just bombing if a runtime exception occurs. A runtime exception is an unexpected result and should not be purposely programmed.
19 years ago
What database are you using? All of the databases that I have used with the exception of Oracle auto commit. I would suggest forcing a roll back if you do not want to commit.
I am attempting to access a result set from a query that has the same column name in both tables. This is on a DB2 database using WebLogic 8.

for example:
select * from TABLE_A A, TABLE_B B where A.columnname = B.columnname

when I attempt to access the result set:
statement.execute();
ResultSet rs = statement.getResultSet();
rs.next();
rs.getDouble("A.column");

I get DB2Exception: [IBM][JDBC Driver] CLI0611E Invalid column name. SQLSTATE=S0022

I also tried fully qualifying the name USER.TABLE_A.columnname, but I received the same error.

Any ideas?

thanks,

Dave
I am getting an error that I can't explain when attempting to execute a finder on my CMP bean. I am using WebLogic Workshop 8 with DB2.

The ejb-ql is:
* @ejbgen:finder max-elements="1000" ejb-ql="SELECT OBJECT(o) from EventBean as o where (o.eventId = ?1 or ?1 = 'BLANK') and (o.eventName LIKE ?2 or ?2 = 'BLANK') and (o.statusCode = ?3 or ?3 = 'Z')" generate-on="Local" signature="Collection finderByCriteria(java.lang.String eventId, java.lang.String eventName, char statusCode)"

The error that I'm getting is:

FinderException Exception in finderByCriteria while preparing or executing statement: 'weblogic.jdbc.wrapper.PreparedStatement_COM_ibm_db2_jdbc_app_DB2PreparedStatement@326' COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2] SQL0302N The value of a host variable in the EXECUTE or OPEN statement is too large for its corresponding use. SQLSTATE=22001

I only get this error when I enter in 6 or more characters into the event name param (?2).

This field is a varchar(30). I get the same problem if I remove the like, or send in %xxxx% as the param. Any idea why I might be getting this error? I tried doing straight SQL into the data base with a similar query, and I was able to execute it and get results.

Some more info on my finder:
The user may enter any pieces of criteria. Thus if they do not enter an eventId I set eventId to 'BLANK' so that the query only checks the other two fields.

Thanks in advance.
--
Dave