• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How can I stream blobs to/from entities?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How can I stream blobs to/from entities?


  • Subject: Re: How can I stream blobs to/from entities?
  • From: Serge Froment <email@hidden>
  • Date: Tue, 13 Apr 2004 15:03:14 -0400

Hi Paolo,

I have some code that works for me. Since it uses undocumented API, I
can't garantee it works in all cases, though. A safer method would be
to ignore WebObjects' database connections and create your own pool of
JDBC connections.

Anyway, here is a class SFGenericRecord
(Stream-Friendly-Generic-Record) that implements a protected method
blobValueForKey. This method fetches the database at JDBC level using
WebObjects's existing connection and returns a java.sql.Blob.

To use it:

1. Add the SFGenericRecord class to your project.
2. Derive you EOF class from SFGenericRecord instead of EOGenericRecord.
3. Don't mark your blob attribute as a class property (so that EOF will
not fetch it).
4. Create an accessor that calls blobValueForKey (from superclass
SFGenericRecord).

To stream the blob to a WOResponse:

Assume "build" is a EO instance that derives from SFGenericRecord and
has an accessor outputFileData that calls
blobValueForKey("outputFileData"):

Blob blob = build.outputFileData();
WOResponse response = new WOResponse();
response.setHeader("data/binary", "Content-Type");
response.setHeader("attachment;filename=" + build.outputFileName(),
"Content-Disposition");
response.setContentStream(blob.getBinaryStream(), 4096, (int)
blob.length());
return response;

And now, the SFGenericRecord class:

// Stream-Friendly subclass of EOGenericRecord
public class SFGenericRecord extends EOGenericRecord
{
	// constructors
	public SFGenericRecord()
	{
		super();
	}
	public SFGenericRecord(EOClassDescription classDescription)
	{
		super(classDescription);
	}

	// fetch a database blob using JDBC
	protected Blob storedBlobForKey(String key)
	{
		EOEntity entity =
((EOEntityClassDescription)classDescription()).entity();
		EODatabaseContext databaseContext =
EODatabaseContext.registeredDatabaseContextForModel(entity.model(),
editingContext());
		databaseContext.lock();
		try
		{
			return getBlob(entity, databaseContext, key);
		}
		catch (Exception e1)
		{
			if (databaseContext._isDroppedConnectionException(e1))
			{
				try
				{
					databaseContext.database().handleDroppedConnection();
					return getBlob(entity, databaseContext, key);
				}
				catch (Exception e2)
				{
					throw new NSForwardException(e2);
				}
			}
			else
				throw new NSForwardException(e1);
		}
		finally
		{
			databaseContext.unlock();
		}
	}

	// actual fetching for blob accessor
	private Blob getBlob(EOEntity entity, EODatabaseContext
databaseContext, String key)
		throws SQLException
	{
		String attributeColumnName = entity.attributeNamed(key).columnName();
		StringBuffer query = new StringBuffer();
		query.append("SELECT ");
		query.append(attributeColumnName);
		query.append(" FROM ");
		query.append(entity.externalName());
		query.append(" WHERE ");
		NSDictionary primaryKey =
EOUtilities.primaryKeyForObject(editingContext(), this);
		Enumeration attributeNames = primaryKey.allKeys().objectEnumerator();
		while (attributeNames.hasMoreElements())
		{
			String attributeName = (String) attributeNames.nextElement();
			query.append(entity.attributeNamed(attributeName).columnName());
			query.append(" = ");
			query.append(primaryKey.valueForKey(attributeName));
			if (attributeNames.hasMoreElements())
				query.append(" AND ");
		}
         EODatabaseChannel databaseChannel =
databaseContext.availableChannel();
         EOAdaptorChannel adaptorChannel =
databaseChannel.adaptorChannel();
         if(!adaptorChannel.isOpen())
             adaptorChannel.openChannel();
		Connection connection =
((JDBCContext)adaptorChannel.adaptorContext()).connection();
		Statement statement = connection.createStatement();
		ResultSet resultSet = statement.executeQuery(query.toString());
		if (resultSet.next())
			return resultSet.getBlob(attributeColumnName);
		else
			throw new SQLException("No record for query " + query);
	}
}

Le 13 avr. 04, ` 10:58, Paolo Sommaruga a icrit :

> metto I have the same problema. Have you reached the WO JDBC
> connection ?
>

Serge Froment
http://www.serge-froment.com
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: How can I stream blobs to/from entities?
      • From: Paolo Sommaruga <email@hidden>
References: 
 >How can I stream blobs to/from entities? (From: Serge Froment <email@hidden>)
 >Re: How can I stream blobs to/from entities? (From: Jonathan Rochkind <email@hidden>)
 >Re: How can I stream blobs to/from entities? (From: Serge Froment <email@hidden>)

  • Prev by Date: Re: Accessing single element from NSArray of objects
  • Next by Date: Re: Accessing single element from NSArray of objects
  • Previous by thread: Re: How can I stream blobs to/from entities?
  • Next by thread: Re: How can I stream blobs to/from entities?
  • Index(es):
    • Date
    • Thread