Alex
Alex

Reputation: 1535

Using Database as Alfresco ContentStore

I'm working with Alfresco 4.2 and I need to use a table in my database as document content store.

Collecting some information hither and thither over the internet, I read that I have to just implement my custom DBContentStore DBContentWriter and DBContentReader classes. Someone told me to take as reference the FileContentStore class.

I need some help to mapping the FileContentStore in order to match my new class.

For example;

The DBContentWriter has to extend AbstractContentWriter and in the API docs I read that the only methods I have to overwrite are:

What about the second method?

protected WritableByteChannel getDirectWritableChannel()

This is called by getContentOutputStream():

public OutputStream getContentOutputStream() throws ContentIOException
    {
        try
        {
            WritableByteChannel channel = getWritableChannel();
            OutputStream is = new BufferedOutputStream(Channels.newOutputStream(channel));
            // done
            return is;
        }

The main method is the putContent(InputStream is) which wants to write content into a DB table.

public final void putContent(InputStream is) throws ContentIOException
{
    try
    {
        OutputStream os = getContentOutputStream();
        copyStreams(is, os); 

Where copyStreams does something like this:

public final int copyStreams(InputStream in, OutputStream out, long sizeLimit) throws IOException
    {
        int byteCount = 0;
        IOException error = null;

        long totalBytesRead = 0;

        try
        {
            byte[] buffer = new byte[BYTE_BUFFER_SIZE];
            int bytesRead = -1;
            while ((bytesRead = in.read(buffer)) != -1)
            {
                // We are able to abort the copy immediately upon limit violation.
                totalBytesRead += bytesRead;
                if (sizeLimit > 0 && totalBytesRead > sizeLimit)
                {
                    StringBuilder msg = new StringBuilder();
                    msg.append("Content size violation, limit = ")
                   .append(sizeLimit);

                    throw new ContentLimitViolationException(msg.toString());
                }

                out.write(buffer, 0, bytesRead);
                byteCount += bytesRead;
            }
            out.flush();
        }
        finally
        {
            try
            {
                in.close();
            }
            catch (IOException e)
            {
                error = e;
                logger.error("Failed to close output stream: " + this, e);
            }
            try
            {
                out.close();
            }
            catch (IOException e)
            {
                error = e;
                logger.error("Failed to close output stream: " + this, e);
            }
        }
        if (error != null)
        {
            throw error;
        }
        return byteCount;
    }
}

The main target is to write some code in order to write and read from the DB using these methods.

When the out.flush() is called i should have to write into the BLOB field.

thanks

Upvotes: 0

Views: 207

Answers (1)

Mardoz
Mardoz

Reputation: 1657

Without looking at the example implementation in FileContentStore it is difficult to determine everything that getDirectWritableChennel() needs to do. Needless to say actually creating a WritableByteChannel to your database should be relatively easy.

Assuming you are using the BLOB type and you are using JDBC to get at your database then you just need to set a stream for your BLOB and turn it in to a channel.

OutputStream stream = myBlob.setBinaryStream(1);
WritableByteChannel channel = Channels.newChannel(stream);

Will you need to overwrite other methods? Maybe. If you have specific issues with those feel free to raise them.

Upvotes: 1

Related Questions